note 70241 added to ref.xml
| From: | aaron dot henderson at gmail dot com | Date: | Sun, 08 Oct 2006 09:57:47 +0000 |
| Subject: | note 70241 added to ref.xml | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-118202@lists.php.net to get a copy of this message | ||
Like others I found the xml2array function by aerik just brilliant, but had a problem with the regex
not getting all the nodes on the same level. After tinkering with the regex and function for a while
I was able to get it to work and get everything into the array.
<?php
function xml2array ($xml)
{
$xmlary = array ();
if ((strlen ($xml) < 256) && is_file ($xml))
$xml = file_get_contents ($xml);
$ReElements =
'/<(\??\w*)\s*\t*([^>]*)(?:\/>|>(.*?)<(\/\s*\1\s*)>)/is';
$ReAttributes = '/(\w+)=(?:"|\')([^"\']*)(:?"|\')/s';
preg_match_all ($ReElements, $xml, $elements);
//print_r($elements);
foreach ($elements[1] as $ie => $xx) {
$xmlary[$ie]["name"] = $elements[1][$ie];
if ( $attributes = trim($elements[2][$ie])) {
preg_match_all ($ReAttributes, $attributes, $att);
foreach ($att[1] as $ia => $xx)
// all the attributes for current element are added here
$xmlary[$ie]["attributes"][$att[1][$ia]] = $att[2][$ia];
} // if $attributes
// get text if it's combined with sub elements
$cdend = strpos($elements[3][$ie],"<");
if ($cdend > 0) {
$xmlary[$ie]["text"] = substr($elements[3][$ie],0,$cdend -1);
} // if cdend
if (preg_match ($ReElements, $elements[3][$ie])){
$xmlary[$ie]["elements"] = xml2array ($elements[3][$ie]);
}
else if (isset($elements[3][$ie])){
$xmlary[$ie]["text"] = $elements[3][$ie];
}
$xmlary[$ie]["closetag"] = $elements[4][$ie];
}//foreach ?
return $xmlary;
}
?>
----
Server IP: 202.89.36.242
Probable Submitter: 222.153.207.1
----
Manual Page -- http://www.php.net/manual/en/ref.xml.php
Edit -- https://master.php.net/note/edit/70241
Del: integrated -- https://master.php.net/note/delete/70241/integrated
Del: useless -- https://master.php.net/note/delete/70241/useless
Del: bad code -- https://master.php.net/note/delete/70241/bad+code
Del: spam -- https://master.php.net/note/delete/70241/spam
Del: non-english -- https://master.php.net/note/delete/70241/non-english
Del: in docs -- https://master.php.net/note/delete/70241/in+docs
Del: other reasons-- https://master.php.net/note/delete/70241
Reject -- https://master.php.net/note/reject/70241
Search -- https://master.php.net/manage/user-notes.php