~imbrandon/+junk/omg-bzr

« back to all changes in this revision

Viewing changes to files/wordpress/wp-content/plugins/google-analytics-for-wordpress/xmlparser.php

  • Committer: Brandon Holtsclaw
  • Date: 2012-05-30 07:31:18 UTC
  • Revision ID: me@brandonholtsclaw.com-20120530073118-5yhyjv4yp8bheiil
speed test

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
function yoast_xml2array($contents, $get_attributes = 1, $priority = 'tag') {
 
3
    if (!function_exists('xml_parser_create'))
 
4
    {
 
5
        return "error";
 
6
    }
 
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);
 
13
    if (!$xml_values)
 
14
        return; //Hmm...
 
15
    $xml_array = array ();
 
16
    $parents = array ();
 
17
    $opened_tags = array ();
 
18
    $arr = array ();
 
19
    $current = & $xml_array;
 
20
    $repeated_tag_index = array (); 
 
21
    foreach ($xml_values as $data)
 
22
    {
 
23
        unset ($attributes, $value);
 
24
        extract($data);
 
25
        $result = array ();
 
26
        $attributes_data = array ();
 
27
        if (isset ($value))
 
28
        {
 
29
            if ($priority == 'tag')
 
30
                $result = $value;
 
31
            else
 
32
                $result['value'] = $value;
 
33
        }
 
34
        if (isset ($attributes) and $get_attributes)
 
35
        {
 
36
            foreach ($attributes as $attr => $val)
 
37
            {
 
38
                if ($priority == 'tag')
 
39
                    $attributes_data[$attr] = $val;
 
40
                else
 
41
                    $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
 
42
            }
 
43
        }
 
44
        if ($type == "open")
 
45
        { 
 
46
            $parent[$level -1] = & $current;
 
47
            if (!is_array($current) or (!in_array($tag, array_keys($current))))
 
48
            {
 
49
                $current[$tag] = $result;
 
50
                if ($attributes_data)
 
51
                    $current[$tag . '_attr'] = $attributes_data;
 
52
                $repeated_tag_index[$tag . '_' . $level] = 1;
 
53
                $current = & $current[$tag];
 
54
            }
 
55
            else
 
56
            {
 
57
                if (isset ($current[$tag][0]))
 
58
                {
 
59
                    $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
 
60
                    $repeated_tag_index[$tag . '_' . $level]++;
 
61
                }
 
62
                else
 
63
                { 
 
64
                    $current[$tag] = array (
 
65
                        $current[$tag],
 
66
                        $result
 
67
                    ); 
 
68
                    $repeated_tag_index[$tag . '_' . $level] = 2;
 
69
                    if (isset ($current[$tag . '_attr']))
 
70
                    {
 
71
                        $current[$tag]['0_attr'] = $current[$tag . '_attr'];
 
72
                        unset ($current[$tag . '_attr']);
 
73
                    }
 
74
                }
 
75
                $last_item_index = $repeated_tag_index[$tag . '_' . $level] - 1;
 
76
                $current = & $current[$tag][$last_item_index];
 
77
            }
 
78
        }
 
79
        elseif ($type == "complete")
 
80
        {
 
81
            if (!isset ($current[$tag]))
 
82
            {
 
83
                $current[$tag] = $result;
 
84
                $repeated_tag_index[$tag . '_' . $level] = 1;
 
85
                if ($priority == 'tag' and $attributes_data)
 
86
                    $current[$tag . '_attr'] = $attributes_data;
 
87
            }
 
88
            else
 
89
            {
 
90
                if (isset ($current[$tag][0]) and is_array($current[$tag]))
 
91
                {
 
92
                    $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
 
93
                    if ($priority == 'tag' and $get_attributes and $attributes_data)
 
94
                    {
 
95
                        $current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
 
96
                    }
 
97
                    $repeated_tag_index[$tag . '_' . $level]++;
 
98
                }
 
99
                else
 
100
                {
 
101
                    $current[$tag] = array (
 
102
                        $current[$tag],
 
103
                        $result
 
104
                    ); 
 
105
                    $repeated_tag_index[$tag . '_' . $level] = 1;
 
106
                    if ($priority == 'tag' and $get_attributes)
 
107
                    {
 
108
                        if (isset ($current[$tag . '_attr']))
 
109
                        { 
 
110
                            $current[$tag]['0_attr'] = $current[$tag . '_attr'];
 
111
                            unset ($current[$tag . '_attr']);
 
112
                        }
 
113
                        if ($attributes_data)
 
114
                        {
 
115
                            $current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
 
116
                        }
 
117
                    }
 
118
                    $repeated_tag_index[$tag . '_' . $level]++; //0 and 1 index is already taken
 
119
                }
 
120
            }
 
121
        }
 
122
        elseif ($type == 'close')
 
123
        {
 
124
            $current = & $parent[$level -1];
 
125
        }
 
126
    }
 
127
    return ($xml_array);
 
128
}
 
129
?>
 
 
b'\\ No newline at end of file'