note 45278 added to ref.simplexml
| From: | cellog@php.net | Date: | Tue, 31 Aug 2004 23:11:19 +0000 |
| Subject: | note 45278 added to ref.simplexml | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-75776@lists.php.net to get a copy of this message | ||
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.
}
?>
----
Manual Page -- http://www.php.net/manual/en/ref.simplexml.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+45278
Delete -- http://master.php.net/manage/user-notes.php?action=delete+45278&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+45278&report=yes
Search -- http://master.php.net/manage/user-notes.php