note 117453 added to function.simplexml-load-string
| From: | artistan at gmail dot com | Date: | Thu, 11 Jun 2015 12:32:12 +0000 |
| Subject: | note 117453 added to function.simplexml-load-string | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-202842@lists.php.net to get a copy of this message | ||
Here is my update to Bob's simple SimpleXML wrapper function.
I noticed his version would turn an empty SimpleXMLElement into an empty array.
<?php
/**
* http://php.net/manual/en/function.simplexml-load-string.php#91564
*
* 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
*
* @param $input
* @param null $callback
* @param bool $recurse
* @return array|mixed
*
*/
function unserialize_xml($input, $callback = null, $recurse = false)
{
// 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){
if(!empty($data)){
$data = (array) $data;
} else {
$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: 72.52.91.14
Probable Submitter: 71.87.10.114
----
Manual Page -- http://php.net/manual/en/function.simplexml-load-string.php
Edit -- https://master.php.net/note/edit/117453
Del: integrated -- https://master.php.net/note/delete/117453/integrated
Del: useless -- https://master.php.net/note/delete/117453/useless
Del: bad code -- https://master.php.net/note/delete/117453/bad+code
Del: spam -- https://master.php.net/note/delete/117453/spam
Del: non-english -- https://master.php.net/note/delete/117453/non-english
Del: in docs -- https://master.php.net/note/delete/117453/in+docs
Del: other reasons-- https://master.php.net/note/delete/117453
Reject -- https://master.php.net/note/reject/117453
Search -- https://master.php.net/manage/user-notes.php