/app/ext/authentication/src/xml2array/xml2array.php

https://github.com/shotdsherrif/Thin-PHP-Framework · PHP · 134 lines · 89 code · 25 blank · 20 comment · 33 complexity · 35065ae8c6131e1d550230b1bc909bf3 MD5 · raw file

  1. <?php
  2. /**
  3. * xml2array() will convert the given XML text to an array in the XML structure.
  4. * Link: http://www.bin-co.com/php/scripts/xml2array/
  5. * Arguments : $contents - The XML text
  6. * $get_attributes - 1 or 0. If this is 1 the function will get the attributes as well as the tag values - this results in a different array structure in the return value.
  7. * $priority - Can be 'tag' or 'attribute'. This will change the way the resulting array sturcture. For 'tag', the tags are given more importance.
  8. * Return: The parsed XML in an array form. Use print_r() to see the resulting array structure.
  9. * Examples: $array = xml2array(file_get_contents('feed.xml'));
  10. * $array = xml2array(file_get_contents('feed.xml', 1, 'attribute'));
  11. */
  12. function xml2array($contents, $get_attributes=1, $priority = 'tag') {
  13. if(!$contents) return array();
  14. if(!function_exists('xml_parser_create')) {
  15. //print "'xml_parser_create()' function not found!";
  16. return array();
  17. }
  18. //Get the XML parser of PHP - PHP must have this module for the parser to work
  19. $parser = xml_parser_create('');
  20. 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
  21. xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
  22. xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
  23. xml_parse_into_struct($parser, trim($contents), $xml_values);
  24. xml_parser_free($parser);
  25. if(!$xml_values) return;//Hmm...
  26. //Initializations
  27. $xml_array = array();
  28. $parents = array();
  29. $opened_tags = array();
  30. $arr = array();
  31. $current = &$xml_array; //Refference
  32. //Go through the tags.
  33. $repeated_tag_index = array();//Multiple tags with same name will be turned into an array
  34. foreach($xml_values as $data) {
  35. unset($attributes,$value);//Remove existing values, or there will be trouble
  36. //This command will extract these variables into the foreach scope
  37. // tag(string), type(string), level(int), attributes(array).
  38. extract($data);//We could use the array by itself, but this cooler.
  39. $result = array();
  40. $attributes_data = array();
  41. if(isset($value)) {
  42. if($priority == 'tag') $result = $value;
  43. else $result['value'] = $value; //Put the value in a assoc array if we are in the 'Attribute' mode
  44. }
  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'
  50. }
  51. }
  52. //See tag status and do the needed.
  53. if($type == "open") {//The starting of the tag '<tag>'
  54. $parent[$level-1] = &$current;
  55. if(!is_array($current) or (!in_array($tag, array_keys($current)))) { //Insert New tag
  56. $current[$tag] = $result;
  57. if($attributes_data) $current[$tag. '_attr'] = $attributes_data;
  58. $repeated_tag_index[$tag.'_'.$level] = 1;
  59. $current = &$current[$tag];
  60. } else { //There was another element with the same tag name
  61. if(isset($current[$tag][0])) {//If there is a 0th element it is already an array
  62. $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
  63. $repeated_tag_index[$tag.'_'.$level]++;
  64. } else {//This section will make the value an array if multiple tags with the same name appear together
  65. $current[$tag] = array($current[$tag],$result);//This will combine the existing item and the new item together to make an array
  66. $repeated_tag_index[$tag.'_'.$level] = 2;
  67. if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well
  68. $current[$tag]['0_attr'] = $current[$tag.'_attr'];
  69. unset($current[$tag.'_attr']);
  70. }
  71. }
  72. $last_item_index = $repeated_tag_index[$tag.'_'.$level]-1;
  73. $current = &$current[$tag][$last_item_index];
  74. }
  75. } elseif($type == "complete") { //Tags that ends in 1 line '<tag />'
  76. //See if the key is already taken.
  77. if(!isset($current[$tag])) { //New Key
  78. $current[$tag] = $result;
  79. $repeated_tag_index[$tag.'_'.$level] = 1;
  80. if($priority == 'tag' and $attributes_data) $current[$tag. '_attr'] = $attributes_data;
  81. } else { //If taken, put all things inside a list(array)
  82. if(isset($current[$tag][0]) and is_array($current[$tag])) {//If it is already an array...
  83. // ...push the new element into that array.
  84. $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
  85. if($priority == 'tag' and $get_attributes and $attributes_data) {
  86. $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
  87. }
  88. $repeated_tag_index[$tag.'_'.$level]++;
  89. } else { //If it is not an array...
  90. $current[$tag] = array($current[$tag],$result); //...Make it an array using using the existing value and the new value
  91. $repeated_tag_index[$tag.'_'.$level] = 1;
  92. if($priority == 'tag' and $get_attributes) {
  93. if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well
  94. $current[$tag]['0_attr'] = $current[$tag.'_attr'];
  95. unset($current[$tag.'_attr']);
  96. }
  97. if($attributes_data) {
  98. $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
  99. }
  100. }
  101. $repeated_tag_index[$tag.'_'.$level]++; //0 and 1 index is already taken
  102. }
  103. }
  104. } elseif($type == 'close') { //End of tag '</tag>'
  105. $current = &$parent[$level-1];
  106. }
  107. }
  108. return($xml_array);
  109. }