note 57864 deleted from ref.simplexml by techtonik
| From: | techtonik@php.net | Date: | Sun, 31 Dec 2006 11:42:01 +0000 |
| Subject: | note 57864 deleted from ref.simplexml by techtonik | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-121130@lists.php.net to get a copy of this message | ||
Note Submitter: t at wantzen dot com
----
Thanks to Daniel Faivre! Great work!
I modified it to an array filled with ISO-8859-1 text and not UTF-8. Haven't tested it in
detail - so be cautious!
Tobias
<?php
/**
* Convert SimpleXMLElement object to ISO array
* Copyright Daniel FAIVRE 2005 - www.geomaticien.com
* Copyleft GPL license
*
* Modifications by Tobias Wantzen
* Returned array will contain single-byte ISO-8859-1 using utf8_decode()
*/
function simplexml2ISOarray($xml) {
if (get_class($xml) == 'SimpleXMLElement') {
$attributes = $xml->attributes();
foreach($attributes as $k=>$v) {
if ($v) $a[$k] = (string) $v;
}
$x = $xml;
$xml = get_object_vars($xml);
}
if (is_array($xml)) {
if (count($xml) == 0) return (string) $x; // for CDATA
foreach($xml as $key=>$value) {
$r[$key] = simplexml2ISOarray($value);
// original line instead of the following if statement:
//$r[$key] = simplexml2ISOarray($value);
if ( !is_array( $r[$key] ) ) $r[$key] = utf8_decode( $r[$key] );
}
if (isset($a)) $r['@'] = $a; // Attributes
return $r;
}
return (string) $xml;
}
?>