PHP xml2array

From Tech
Jump to: navigation, search

There is a handy xml2array function that can make it very easy to work with XML on PHP. One potential use for this would be getting information from web pages.

The output array will not be in the same order as the input elements. It will be sorted by tag type.

Example

Here is an example of using curl to get a web page, then turning that page into an array.

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "guodman.org");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
var_dump(xml2array($output));

Input HTML

<html>
<head>
<title>Guodman's Pages</title>
</head>
<body>
<h1>Sites</h1>
<a href="/tech/">Tech Wiki</a><br />
<h1>Configuration Files</h1>
<a href=".emacs">Emacs Configuration</a><br />
<a href=".screenrc">Screen Configuration</a><br />
<a href="sources.list">Ubuntu Repo List</a><br />
<a href="Zenburn.colorscheme">Zenburn Color Scheme</a><br />
</body></html>

Output Array

array(1) {
  ["html"]=>
  array(2) {
    ["head"]=>
    array(1) {
      ["title"]=>
      string(15) "Guodman's Pages"
    }
    ["body"]=>
    array(3) {
      ["h1"]=>
      array(2) {
        [0]=>
        string(5) "Sites"
        [1]=>
        string(19) "Configuration Files"
      }
      ["a"]=>
      array(10) {
        [0]=>
        string(9) "Tech Wiki"
        [1]=>
        string(19) "Emacs Configuration"
        ["0_attr"]=>
        array(1) {
          ["href"]=>
          string(6) "/tech/"
        }
        ["1_attr"]=>
        array(1) {
          ["href"]=>
          string(6) ".emacs"
        }
        [2]=>
        string(20) "Screen Configuration"
        ["2_attr"]=>
        array(1) {
          ["href"]=>
          string(9) ".screenrc"
        }
        [3]=>
        string(16) "Ubuntu Repo List"
        ["3_attr"]=>
        array(1) {
          ["href"]=>
          string(12) "sources.list"
        }
        [4]=>
        string(20) "Zenburn Color Scheme"
        ["4_attr"]=>
        array(1) {
          ["href"]=>
          string(19) "Zenburn.colorscheme"
        }
      }
      ["br"]=>
      array(5) {
        [0]=>
        array(0) {
        }
        [1]=>
        array(0) {
        }
        [2]=>
        array(0) {
        }
        [3]=>
        array(0) {
        }
        [4]=>
        array(0) {
        }
      }
    }
  }
}
Personal tools