note 61450 added to function.simplexml-load-file
| From: | skutter at imprecision dot net | Date: | Fri, 03 Feb 2006 17:11:41 +0000 |
| Subject: | note 61450 added to function.simplexml-load-file | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-102938@lists.php.net to get a copy of this message | ||
So it seems SimpleXML doesn't support CDATA... I bashed together this little regex function to
sort out the CDATA before trying to parse XML with the likes of simplexml_load_file /
simplexml_load_string. Hope it might help somebody and would be very interested to hear of better
solutions. (Other than *not* using SimpleXML of course! ;)
It looks for any <![CDATA [Text and HTML etc in here]]> elements, htmlspecialchar()'s the
encapsulated data and then strips the "<![CDATA [" and "]]>" tags out.
<?php
function simplexml_unCDATAise($xml) {
$new_xml = NULL;
preg_match_all("/\<\!\[CDATA \[(.*)\]\]\>/U", $xml, $args);
if (is_array($args)) {
if (isset($args[0]) && isset($args[1])) {
$new_xml = $xml;
for ($i=0; $i<count($args[0]); $i++) {
$old_text = $args[0][$i];
$new_text = htmlspecialchars($args[1][$i]);
$new_xml = str_replace($old_text, $new_text, $new_xml);
}
}
}
return $new_xml;
}
//Usage:
$xml = 'Your XML with CDATA...';
$xml = simplexml_unCDATAise($xml);
$xml_object = simplexml_load_string($xml);
?>
----
Server IP: 64.246.30.37
Probable Submitter: 80.77.243.32
----
Manual Page -- http://www.php.net/manual/en/function.simplexml-load-file.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+61450
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+61450&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+61450&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+61450&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+61450&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+61450&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+61450&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+61450&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+61450&report=yes
Search -- http://master.php.net/manage/user-notes.php