note 91564 added to function.simplexml-load-string
| From: | Bob at osu1 dot php dot net | Date: | Wed, 17 Jun 2009 09:16:42 +0000 |
| Subject: | note 91564 added to function.simplexml-load-string | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-156956@lists.php.net to get a copy of this message | ||
Here is my simple SimpleXML wrapper function.
As far as I can tell, it does the same as Julio Cesar Oliveira's (above).
It parses an XML string into a multi-dimensional associative array.
The second argument is a callback that is run on all data (so for example, if you want all data
trimmed, like Julio does in his function, just pass 'trim' as the second arg).
<?php
function unserialize_xml($input, $callback = null, $recurse = false)
/* bool/array unserialize_xml ( string $input [ , callback $callback ] )
* Unserializes an XML string, returning a multi-dimensional associative array, optionally runs a
callback on all non-array data
* Returns false on all failure
* Notes:
* Root XML tags are stripped
* Due to its recursive nature, unserialize_xml() will also support SimpleXMLElement objects and
arrays as input
* Uses simplexml_load_string() for XML parsing, see SimpleXML documentation for more info
*/
{
// Get input, loading an xml string with simplexml if its the top level of recursion
$data = ((!$recurse) && is_string($input))? simplexml_load_string($input): $input;
// Convert SimpleXMLElements to array
if ($data instanceof SimpleXMLElement) $data = (array) $data;
// Recurse into arrays
if (is_array($data)) foreach ($data as &$item) $item = unserialize_xml($item, $callback,
true);
// Run callback and return
return (!is_array($data) && is_callable($callback))? call_user_func($callback, $data):
$data;
}
?>
----
Server IP: 117.104.162.229
Probable Submitter: 58.96.44.86 (proxied: 58.96.44.86)
----
Manual Page -- http://www.php.net/manual/en/function.simplexml-load-string.php
Edit -- https://master.php.net/note/edit/91564
Del: integrated -- https://master.php.net/note/delete/91564/integrated
Del: useless -- https://master.php.net/note/delete/91564/useless
Del: bad code -- https://master.php.net/note/delete/91564/bad+code
Del: spam -- https://master.php.net/note/delete/91564/spam
Del: non-english -- https://master.php.net/note/delete/91564/non-english
Del: in docs -- https://master.php.net/note/delete/91564/in+docs
Del: other reasons-- https://master.php.net/note/delete/91564
Reject -- https://master.php.net/note/reject/91564
Search -- https://master.php.net/manage/user-notes.php