2
function yoast_xml2array($contents, $get_attributes = 1, $priority = 'tag') {
3
if (!function_exists('xml_parser_create'))
7
$parser = xml_parser_create('');
8
xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8");
9
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
10
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
11
xml_parse_into_struct($parser, trim($contents), $xml_values);
12
xml_parser_free($parser);
15
$xml_array = array ();
17
$opened_tags = array ();
19
$current = & $xml_array;
20
$repeated_tag_index = array ();
21
foreach ($xml_values as $data)
23
unset ($attributes, $value);
26
$attributes_data = array ();
29
if ($priority == 'tag')
32
$result['value'] = $value;
34
if (isset ($attributes) and $get_attributes)
36
foreach ($attributes as $attr => $val)
38
if ($priority == 'tag')
39
$attributes_data[$attr] = $val;
41
$result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
46
$parent[$level -1] = & $current;
47
if (!is_array($current) or (!in_array($tag, array_keys($current))))
49
$current[$tag] = $result;
51
$current[$tag . '_attr'] = $attributes_data;
52
$repeated_tag_index[$tag . '_' . $level] = 1;
53
$current = & $current[$tag];
57
if (isset ($current[$tag][0]))
59
$current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
60
$repeated_tag_index[$tag . '_' . $level]++;
64
$current[$tag] = array (
68
$repeated_tag_index[$tag . '_' . $level] = 2;
69
if (isset ($current[$tag . '_attr']))
71
$current[$tag]['0_attr'] = $current[$tag . '_attr'];
72
unset ($current[$tag . '_attr']);
75
$last_item_index = $repeated_tag_index[$tag . '_' . $level] - 1;
76
$current = & $current[$tag][$last_item_index];
79
elseif ($type == "complete")
81
if (!isset ($current[$tag]))
83
$current[$tag] = $result;
84
$repeated_tag_index[$tag . '_' . $level] = 1;
85
if ($priority == 'tag' and $attributes_data)
86
$current[$tag . '_attr'] = $attributes_data;
90
if (isset ($current[$tag][0]) and is_array($current[$tag]))
92
$current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
93
if ($priority == 'tag' and $get_attributes and $attributes_data)
95
$current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
97
$repeated_tag_index[$tag . '_' . $level]++;
101
$current[$tag] = array (
105
$repeated_tag_index[$tag . '_' . $level] = 1;
106
if ($priority == 'tag' and $get_attributes)
108
if (isset ($current[$tag . '_attr']))
110
$current[$tag]['0_attr'] = $current[$tag . '_attr'];
111
unset ($current[$tag . '_attr']);
113
if ($attributes_data)
115
$current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
118
$repeated_tag_index[$tag . '_' . $level]++; //0 and 1 index is already taken
122
elseif ($type == 'close')
124
$current = & $parent[$level -1];
b'\\ No newline at end of file'