note 92785 added to function.xml-parse

From: Date: Sun, 09 Aug 2009 05:59:17 +0000
Subject: note 92785 added to function.xml-parse
Groups: php.notes 
Request: Send a blank email to php-notes+get-159224@lists.php.net to get a copy of this message
//Simple XML Parser. Ignoring blank keys or flang keys so you can use it with eval("$key = value;"); //For example: //<Order><Amount>99</Amount></Order> // "Order" is ignored, "Amount" = key, "99" = value //Output array (eliminate bottom portion): //[0] = "$Amount" [1] = "99" // OR //['Amount'] = "99" //output array with keys (even) and values (odd) 0 = $key 1 = value 2 = $key ... or ['key'] = value function parse_xml($arg){ $arg = str_replace("&lt;","<",$arg); $arg = str_replace("&gt;",">",$arg); $r = false; $k = false; $t = ""; $s = ""; $results = array(); //composing straight array of keys (even) followed by values (odd) [0] = key [1] = value for($f = 0;$f < strlen($arg); $f++){ $x = substr($arg, $f, 1); if($x == "<"){//key set $r = true; $k = false; $s = parse_xml_clean_str($s); if($s != ""){ if(substr($results[count($results) - 1],0,1) == '$') array_pop($results); array_push($results, '$'.$s); $s = ""; }else{ $t = ""; } } if($x == ">"){//value set $r = true; $k = true; $t = parse_xml_clean_str($t); if($t != ""){ array_push($results, $t); $t = ""; } } if($r && $k) $t .= $x;//record value if($r && !$k){ if($x == "/" || $x == "?"){ $r = false; $k = false; }else{ $s .= $x;//record key } } } //composing array of key equal value ['key']=value if($results){ $results_key_value = array(); for($f = 0;$f < count($results); $f++) if($f % 2 == 0) $results_key_value[str_replace("$","",$results[$f])] = $results[$f + 1]; $results = $results_key_value; } return $results; } function parse_xml_clean_str($x){ $x = str_replace("<","",$x); $x = str_replace(">","",$x); $x = str_replace("\r","",$x); $x = str_replace("\n","",$x); $x = trim($x); return $x; } ---- Server IP: 209.41.74.194 Probable Submitter: 209.210.48.118 ---- Manual Page -- http://www.php.net/manual/en/function.xml-parse.php Edit -- https://master.php.net/note/edit/92785 Del: integrated -- https://master.php.net/note/delete/92785/integrated Del: useless -- https://master.php.net/note/delete/92785/useless Del: bad code -- https://master.php.net/note/delete/92785/bad+code Del: spam -- https://master.php.net/note/delete/92785/spam Del: non-english -- https://master.php.net/note/delete/92785/non-english Del: in docs -- https://master.php.net/note/delete/92785/in+docs Del: other reasons-- https://master.php.net/note/delete/92785 Reject -- https://master.php.net/note/reject/92785 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#159224) next »