note 66182 deleted from ref.xml by bjori

From: Date: Tue, 16 May 2006 21:55:50 +0000
Subject: note 66182 deleted from ref.xml by bjori
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-111416@lists.php.net to get a copy of this message
Note Submitter: n15 at free dot fr Reason: useless ---- --xml2array part4-- After vladson, guyemup, HansP, BinnyVA, aerik, adrianus and sander ... here's my contrib. for the mythic xml2array with 'no library'. It corrects multi tags and ... impl. CDATA ... with no recursivity. ex: <code id="code1"> <elements> <elements> <elements>text1</elements> <elements>text2</elements> <elements><![CDATA[A bunch of <a href="#">code</a>]]></elements> </elements> </elements> </code> <? function xml2array($xml) { $xmlary = array (); $fCdata = false; // flag CDATA $RgE = '/<(\/|\!\[CDATA\[)?(\w+)((?:[^">]*|"[^"]*")*)(?:\/>|>)([^<]*)/'; preg_match_all ($RgE, $xml, $elements); $ReAttributes = '/(\w+)=(?:"|\')([^"\']*)(:?"|\')/'; $level = -1; foreach ($elements[1] as $ie => $xx) { if($fCdata) { if(substr($elements[0][$ie], strlen($elements[0][$ie])-3)==']]>') { $fCdata = false; $tmpary["text"] .= substr($elements[0][$ie],0, strlen($elements[0][$ie])-3); } else $tmpary["text"] .= $elements[0][$ie]; continue; } $refary = &$xmlary[$ie?count($xmlary)-1:0]; for($i=0;$i<$level;$i++) $refary = &$refary["elements"][count($refary["elements"])-1]; if(!$xx) { // start Tag $level++; if($ie>0) $refary = &$refary["elements"][count($refary["elements"])]; $refary["name"] = $elements[2][$ie]; if ( $attributes = trim($elements[3][$ie])) { preg_match_all ($ReAttributes, $attributes, $att); foreach ($att[1] as $ia => $xx) // all the attributes for current element are added here $refary["attributes"][$att[1][$ia]] = $att[2][$ia]; } // if $attributes if($elements[4][$ie]) //if content... $refary["text"] = $elements[4][$ie]; } else if($xx=='/') { // end Tag $level--; $refary["closetag"] = $elements[2][$ie]; } else { // $xx == '![CDATA[' $refary["text"] = substr($elements[0][$ie],9); $fCdata = true; } }//foreach return $xmlary; } ?> next one

« previous php.notes (#111416) next »