2 function xml2array($contents, $get_attributes=1, $priority = 'tag') {
3 if(!$contents) return array();
5 if(!function_exists('xml_parser_create')) {
6 //print "'xml_parser_create()' function not found!";
10 //Get the XML parser of PHP - PHP must have this module for the parser to work
11 $parser = xml_parser_create('');
12 xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8"); # http://minutillo.com/steve/weblog/2004/6/17/php-xml-and-character-encodings-a-tale-of-sadness-rage-and-data-loss
13 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
14 xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
15 xml_parse_into_struct($parser, trim($contents), $xml_values);
16 xml_parser_free($parser);
18 if(!$xml_values) return;//Hmm...
23 $opened_tags = array();
26 $current = &$xml_array; //Refference
28 //Go through the tags.
29 $repeated_tag_index = array();//Multiple tags with same name will be turned into an array
30 foreach($xml_values as $data) {
31 unset($attributes,$value);//Remove existing values, or there will be trouble
33 //This command will extract these variables into the foreach scope
34 // tag(string), type(string), level(int), attributes(array).
35 extract($data);//We could use the array by itself, but this cooler.
38 $attributes_data = array();
41 if($priority == 'tag') $result = $value;
42 else $result['value'] = $value; //Put the value in a assoc array if we are in the 'Attribute' mode
45 //Set the attributes too.
46 if(isset($attributes) and $get_attributes) {
47 foreach($attributes as $attr => $val) {
48 if($priority == 'tag') $attributes_data[$attr] = $val;
49 else $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
53 //See tag status and do the needed.
54 if($type == "open") {//The starting of the tag '<tag>'
55 $parent[$level-1] = &$current;
56 if(!is_array($current) or (!in_array($tag, array_keys($current)))) { //Insert New tag
57 $current[$tag] = $result;
58 if($attributes_data) $current[$tag. '_attr'] = $attributes_data;
59 $repeated_tag_index[$tag.'_'.$level] = 1;
61 $current = &$current[$tag];
63 } else { //There was another element with the same tag name
65 if(isset($current[$tag][0])) {//If there is a 0th element it is already an array
66 $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
67 $repeated_tag_index[$tag.'_'.$level]++;
68 } else {//This section will make the value an array if multiple tags with the same name appear together
69 $current[$tag] = array($current[$tag],$result);//This will combine the existing item and the new item together to make an array
70 $repeated_tag_index[$tag.'_'.$level] = 2;
72 if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well
73 $current[$tag]['0_attr'] = $current[$tag.'_attr'];
74 unset($current[$tag.'_attr']);
78 $last_item_index = $repeated_tag_index[$tag.'_'.$level]-1;
79 $current = &$current[$tag][$last_item_index];
82 } elseif($type == "complete") { //Tags that ends in 1 line '<tag />'
83 //See if the key is already taken.
84 if(!isset($current[$tag])) { //New Key
85 $current[$tag] = $result;
86 $repeated_tag_index[$tag.'_'.$level] = 1;
87 if($priority == 'tag' and $attributes_data) $current[$tag. '_attr'] = $attributes_data;
89 } else { //If taken, put all things inside a list(array)
90 if(isset($current[$tag][0]) and is_array($current[$tag])) {//If it is already an array...
92 // ...push the new element into that array.
93 $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
95 if($priority == 'tag' and $get_attributes and $attributes_data) {
96 $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
98 $repeated_tag_index[$tag.'_'.$level]++;
100 } else { //If it is not an array...
101 $current[$tag] = array($current[$tag],$result); //...Make it an array using using the existing value and the new value
102 $repeated_tag_index[$tag.'_'.$level] = 1;
103 if($priority == 'tag' and $get_attributes) {
104 if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well
106 $current[$tag]['0_attr'] = $current[$tag.'_attr'];
107 unset($current[$tag.'_attr']);
110 if($attributes_data) {
111 $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
114 $repeated_tag_index[$tag.'_'.$level]++; //0 and 1 index is already taken
118 } elseif($type == 'close') { //End of tag '</tag>'
119 $current = &$parent[$level-1];