PluginProbe
WPGlobus / 3.0.5
WPGlobus v3.0.5
3.0.5 3.0.4 trunk 2.1.15 2.10.10 2.12.2 2.2.35 2.3.12 2.4.17 2.5.23 2.6.8 2.7.14 2.8.11 3.0.0 3.0.1 3.0.2 3.0.3
wpglobus / lib / xml2array.php

xml2array.php in WPGlobus 3.0.5, at lib/xml2array.php

100 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit; // Exit if accessed directly
5 }
6
7 if(!function_exists('icl_xml2array')):
8
9 function icl_xml2array($contents, $get_attributes=1) {
10 if(!$contents) return array();
11
12 if(!function_exists('xml_parser_create')) {
13 //print "'xml_parser_create()' function not found!";
14 return array();
15 }
16 //Get the XML parser of PHP - PHP must have this module for the parser to work
17 $parser = xml_parser_create();
18 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, 0 );
19 xml_parser_set_option( $parser, XML_OPTION_SKIP_WHITE, 1 );
20 xml_parse_into_struct( $parser, $contents, $xml_values );
21 xml_parser_free( $parser );
22
23 if(!$xml_values) return;//Hmm...
24
25 //Initializations
26 $xml_array = array();
27 $parents = array();
28 $opened_tags = array();
29 $arr = array();
30
31 $current = &$xml_array;
32
33 //Go through the tags.
34 foreach($xml_values as $data) {
35 unset($attributes,$value);//Remove existing values, or there will be trouble
36
37 //This command will extract these variables into the foreach scope
38 // tag(string), type(string), level(int), attributes(array).
39 extract($data);//We could use the array by itself, but this cooler.
40 //TODO [WPML 3.2] stop using extract($data)
41
42 $result = '';
43 if($get_attributes) {//The second argument of the function decides this.
44 $result = array();
45 if(isset($value)) $result['value'] = $value;
46
47 //Set the attributes too.
48 if(isset($attributes)) {
49 foreach($attributes as $attr => $val) {
50 if($get_attributes == 1) $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
51 //TODO: [WPML 3.2] should we change the key name to 'wpml_attr'? Someone may use the tagname 'attr'. Same goes for 'value' too */
52 }
53 }
54 } elseif(isset($value)) {
55 $result = $value;
56 }
57
58 //See tag status and do the needed.
59 if($type == "open") {//The starting of the tag '<tag>'
60 $parent[$level-1] = &$current;
61
62 if(!is_array($current) or (!in_array($tag, array_keys($current)))) { //Insert New tag
63 $current[$tag] = $result;
64 $current = &$current[$tag];
65
66 } else { //There was another element with the same tag name
67 if(isset($current[$tag][0])) {
68 array_push($current[$tag], $result);
69 } else {
70 $current[$tag] = array($current[$tag],$result);
71 }
72 $last = count($current[$tag]) - 1;
73 $current = &$current[$tag][$last];
74 }
75
76 } elseif($type == "complete") { //Tags that ends in 1 line '<tag />'
77 //See if the key is already taken.
78 if(!isset($current[$tag])) { //New Key
79 $current[$tag] = $result;
80
81 } else { //If taken, put all things inside a list(array)
82 if((is_array($current[$tag]) and $get_attributes == 0)//If it is already an array...
83 or (isset($current[$tag][0]) and is_array($current[$tag][0]) and $get_attributes == 1)) {
84 array_push($current[$tag],$result); // ...push the new element into that array.
85 } else { //If it is not an array...
86 $current[$tag] = array($current[$tag],$result); //...Make it an array using using the existing value and the new value
87 }
88 }
89
90 } elseif($type == 'close') { //End of tag '</tag>'
91 $current = &$parent[$level-1];
92 }
93 }
94
95 return($xml_array);
96 }
97
98 endif;
99
100