note 70244 added to ref.xml
| From: | Rattus at osu1 dot php dot net | Date: | Sun, 08 Oct 2006 11:48:16 +0000 |
| Subject: | note 70244 added to ref.xml | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-118208@lists.php.net to get a copy of this message | ||
Oops!
My last posting had a typo and I have since decided to separate the file reading function from the
xml conversion routine.
EXAMPLE:
--------
<?php
print_r(xml2array(readXMLFile('file.xml')));
?>
FUNCTION DEFINITIONS:
---------------------
<?php
function readXMLFile($xmlFilename)
{
if (is_file($xmlFilename))
{
$xml = file_get_contents($xmlFilename); // load file and remove unnecessary white-space
$xml = preg_replace('@[\t\r\n]+@', '', $xml);
$xml = preg_replace('@>\s+@', '>', $xml);
$xml = preg_replace('@\s+<@', '<', $xml);
return $xml;
}
return false;
}
function xml2array($xml)
{
if (!is_string($xml)) return false;
$xmlary = array();
$ReElements =
'/<\s*(\w+)\s*([^\/>]*)\s*(?:\/>|>(.*?)<(\/\s*\1\s*)>)/s';
$ReAttributes = '/(\w+)=(?:"|\')([^"\']*)(:?"|\')/';
preg_match_all($ReElements, $xml, $elements);
$index = 0;
foreach($elements[1] as $ie => $xx)
{
if ($attributes = trim($elements[2][$ie]))
{
preg_match_all ($ReAttributes, $attributes, $att);
foreach ($att[1] as $ia => $yy) $xmlary["attributes"][$att[1][$ia]] = $att[2][$ia];
// all the attributes for current element are added here
}
$cdend = strpos($elements[3][$ie], "<"); // get text if it's combined with
sub elements
if ($cdend > 0) $xmlary["text"] = substr($elements[3][$ie], 0, $cdend);
if (preg_match($ReElements, $elements[3][$ie])) $xmlary[$elements[1][$ie]][$index++] = xml2array
($elements[3][$ie]);
else if (isset($elements[3][$ie])) $xmlary[$elements[1][$ie]] = $elements[3][$ie];
}
return $xmlary;
}
?>
----
Server IP: 203.220.0.234
Probable Submitter: 218.215.0.156
----
Manual Page -- http://www.php.net/manual/en/ref.xml.php
Edit -- https://master.php.net/note/edit/70244
Del: integrated -- https://master.php.net/note/delete/70244/integrated
Del: useless -- https://master.php.net/note/delete/70244/useless
Del: bad code -- https://master.php.net/note/delete/70244/bad+code
Del: spam -- https://master.php.net/note/delete/70244/spam
Del: non-english -- https://master.php.net/note/delete/70244/non-english
Del: in docs -- https://master.php.net/note/delete/70244/in+docs
Del: other reasons-- https://master.php.net/note/delete/70244
Reject -- https://master.php.net/note/reject/70244
Search -- https://master.php.net/manage/user-notes.php