File indexing completed on 2025-01-12 05:18:48
0001 <?php 0002 0003 /* 0004 * GFX 4 0005 * 0006 * support: happy.snizzo@gmail.com 0007 * website: http://trt-gfx.googlecode.com 0008 * credits: Claudio Desideri 0009 * 0010 * This software is released under the MIT License. 0011 * http://opensource.org/licenses/mit-license.php 0012 */ 0013 0014 /** 0015 * to_array() will convert the given XML text to an array in the XML structure 0016 */ 0017 0018 class EXmlParser{ 0019 0020 public static function to_array($contents, $force_multiple=array()) { 0021 if(!$contents) return array(); 0022 0023 if(!function_exists('xml_parser_create')) { 0024 //print "'xml_parser_create()' function not found!"; 0025 return array(); 0026 } 0027 0028 //Get the XML parser of PHP - PHP must have this module for the parser to work 0029 $parser = xml_parser_create(''); 0030 xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8"); 0031 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); 0032 xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1); 0033 xml_parse_into_struct($parser, trim($contents), $xml_values); 0034 xml_parser_free($parser); 0035 0036 if(!$xml_values) return; 0037 0038 $data = EXmlParser::parse_array(array(),$xml_values,$force_multiple); 0039 0040 return $data; 0041 } 0042 0043 /** 0044 * Convert an xml tree to an associative array, supporting also duplicate keys 0045 * 0046 * @input $data give always an empty array 0047 * @input &$tags declarative array generated from xml_parse_into_struct() 0048 * @returns $data associative array 0049 */ 0050 public static function parse_array($data,&$tags,$force_multiple=array()){ 0051 do{ 0052 if(is_array($tags)){ 0053 $entry = array_shift($tags); 0054 } else { 0055 return array(); 0056 } 0057 0058 if(isset($entry["tag"])){ $tag = $entry["tag"]; } 0059 if(isset($entry["type"])){ $type = $entry["type"]; } 0060 if(isset($entry["value"])){ $value = $entry["value"]; } else { $value = ''; } 0061 0062 //just add value 0063 if($type=='complete') { 0064 $data[$tag] = $value; 0065 } 0066 0067 //open new tag 0068 if($type=='open'){ 0069 if(isset($data[$tag])){ 0070 //if array is associative, it means it's not a collection of nodes with the same name 0071 if(EXmlParser::is_assoc($data[$tag])){ 0072 $prev = $data[$tag]; 0073 $new = EXmlParser::parse_array(array(),$tags,$force_multiple); 0074 $data[$tag] = array(); 0075 $data[$tag][] = $prev; 0076 $data[$tag][] = $new; 0077 } else { //it's already a collection 0078 $new = EXmlParser::parse_array(array(),$tags,$force_multiple); 0079 $data[$tag][] = $new; 0080 } 0081 0082 } else { 0083 if (is_array($force_multiple) and in_array($tag, $force_multiple)) { 0084 $data[$tag] = array(); 0085 $data[$tag][] = EXmlParser::parse_array($data[$tag],$tags,$force_multiple); 0086 } else { 0087 $data[$tag] = array(); 0088 $data[$tag] = EXmlParser::parse_array($data[$tag],$tags,$force_multiple); 0089 } 0090 } 0091 } 0092 0093 //close tags 0094 if($type=='close') { 0095 return $data; 0096 } 0097 0098 }while(count($tags)>0); 0099 0100 return $data; 0101 } 0102 0103 /* 0104 * Check if the given array is associative (key => value) or declarative (value, value, value) 0105 */ 0106 public static function is_assoc($array) { 0107 return (bool)count(array_filter(array_keys($array), 'is_string')); 0108 } 0109 0110 }