note 92785 modified in function.xml-parse by felipe

From: Date: Sun, 09 Aug 2009 15:19:55 +0000
Subject: note 92785 modified in function.xml-parse by felipe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-159235@lists.php.net to get a copy of this message
<?php //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; } ?> --was-- //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; } http://php.net/manual/en/function.xml-parse.php

« previous php.notes (#159235) next »