Voting

: three plus five?
(Example: nine)

The Note You're Voting On

cellog at php dot net
20 years ago
simplexml does not simply handle CDATA sections in a foreach loop.

<?php
$sx
= simplexml_load_string('
<test>
<one>hi</one>
<two><![CDATA[stuff]]></two>
<t>
<for>two</for>
</t>
<multi>one</multi>
<multi>two</multi>
</test>'
);
foreach((array)
$sx as $tagname => $val) {
if (
is_string($val)) {
// <one> will go here
} elseif (is_array($val)) {
// <multi> will go here because it happens multiple times
} elseif (is_object($val)) {
// <t> will go here because it contains tags
// <two> will go here because it contains CDATA!
}
}
?>

To test in the loop, do this

<?php
if (count((array) $val) == 0) {
// this is not a tag that contains other tags
$val = '' . $val;
// now the CDATA is revealed magically.
}
?>

<< Back to user notes page

To Top