Wow, Check Out This XML2Array Function
Posted: Mon Nov 17, 2008 4:14 pm
It doesn't get any easier than this.
Now, it won't ignore comments and only goes one level deep, but it's pretty darn cool, none-the-less.
Code: Select all
function XML2Array($s) {
preg_match_all("/(<([\w]+)[^>]*>)(.*)(<\/\\2>)/", $s, $asMatches, PREG_SET_ORDER);
$asItems = array();
foreach ($asMatches as $sVal) {
$oItem = (object) array();
$oItem->Full = $sVal[0];
$oItem->Before = $sVal[1];
$oItem->Tag = $sVal[2];
$oItem->Value = $sVal[3];
$oItem->After = $sVal[4];
array_push($asItems, (array) $oItem);
}
return $asItems;
}