/    Sign up×
Community /Pin to ProfileBookmark

Can PHP Read "CData" Info?

Hello again.

Lets say that was reading an XML file and I wanted to pull and save to a variable the text only info in between these tags:

[CODE]

<description><![CDATA[ [I][U]some text, some text, and some text[/U][/I] […]]]></description>

[/CODE]

How would I go about doing that with PHP? Is it even possible?

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@tracknutJul 21.2010 — Sure, I'd use SimpleXML:

<i>
</i>$x = simplexml_load_file ("content.xml");
$desc = $x-&gt;description;


The CDATA begin/end stuff isn't going to end up in $desc.

Dave
Copy linkTweet thisAlerts:
@NogDogJul 21.2010 — You should be able to access it via the [url=http://php.net/dom]DOM classes[/url], e.g.:
[code=php]
<?php
$xml = <<<EOD
<?xml version='1.0' ?>
<document>
<thing>
<description><![CDATA[The first thing]]></description>
</thing>
<thing>
<description><![CDATA[The second thing]]></description>
</thing>
</document>
EOD;

$dom = new DOMDocument();
$dom->loadXML($xml);
$descriptions = $dom->getElementsByTagName('description');
foreach($descriptions as $desc)
{
echo "<p>".$desc->textContent."</p>n";
}[/code]
Copy linkTweet thisAlerts:
@ChuckBauthorJul 21.2010 — ...alright..appreciate it...I just thought the "CDATA" stuff would get in the way...
×

Success!

Help @ChuckB spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...