- <?php
- $str =<<<EOF
- <package packagerversion="1.8.1" version="2.0" xmlns="http://pear.php.net/dtd/package-2.0" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0 http://pear.php.net/dtd/tasks-1.0.xsd http://pear.php.net/dtd/package-2.0 http://pear.php.net/dtd/package-2.0.xsd">
- <name>symfony</name>
- <channel>pear.symfony-project.com</channel>
- <summary>Symfony is a complete framework designed to optimize the development of web applications.</summary>
- <description>Symfony is a complete framework designed to optimize the development of web applications by way of several key features.
- For starters, it separates a web application's business rules, server logic, and presentation views.
- It contains numerous tools and classes aimed at shortening the development time of a complex web application.
- Additionally, it automates common tasks so that the developer can focus entirely on the specifics of an application.
- The end result of these advantages means there is no need to reinvent the wheel every time a new web application is built!</description>
- <lead>
- <name>Fabien POTENCIER</name>
- <user>fabpot</user>
- <email>fabien.potencier@symfony-project.com</email>
- <active>yes</active>
- </lead>
- <date>2011-09-16</date>
- <time>11:37:29</time>
- <version>
- <release>1.4.14</release>
- <api>1.4.0</api>
- </version>
- <stability>
- <release>stable</release>
- <api>stable</api>
- </stability>
- EOF;
- //xml is a file
- //xml is a string
- //return a php array
- class phpXMLParser{
- //default
- const XMLFILE = true;
- const XMLSTR = false;
- private static $phparr = array();
- private static $phparrTemplate = "";
- private static $xmlstring = '';
- public function __construct($xml, $type = true)
- {
- self::setXML($xml, $type);
- }
- //set xml file of string
- static public function setXML($xml, $type)
- {
- if (true === $type) {
- if(file_exists($xml))
- {
- self::$xmlstring = file_get_contents($xml);
- }
- }
- else if(false === $type)
- {
- if (is_string($xml)) {
- self::$xmlstring = $xml;
- }
- }
- }
- static public function setTemplate($template = "")
- {
- self::$phparrTemplate = $template;
- }
- //get xml
- static public function getXML()
- {
- return self::$xmlstring;
- }
- //parse xml return an PHP array
- static public function run()
- {
- return self::parseXML(self::getXML());
- }
- private function parseXML($xml)
- {
- // parsexml
- preg_match_all("/<(\w+)>/s", $xml ,$tags);
- /*<sss><dd>sss</dd><ff>eee</fff></sss>
- preg_match_all("/<.*?>.*?.(\w+|\w*).*?<\/.*?>/s", $xml, $contents);
- */
- $arr = array();
- foreach (end($tags) as $val) {
- preg_match("/<$val>(.*?)<\/$val>/s", $xml, $arr[]);
- }
- foreach ($arr as $key1 => & $val1) {
- $val1 = end($val1);
- }
- self::$phparr = array_combine(end($tags), $arr);
- /*
- // create a PHP file
- if (!file_exists($file)) {
- file_put_contents($file, var_export($phparr, true));
- }
- else {
- self::$phparr = include($file);
- }
- */
- return self::$phparr;
- }
- }
- $xmlparser = new phpXMLParser($str, false);
- var_dump($xmlparser::run());
- exit;
- //phpXMLParser::run();
- function readxml($xml)
- {
- $arr = array();
- preg_match_all("/<(\w+)>/", $xml ,$k);
- /*
- preg_match_all("/<.*?>(.*?)<\/.*?>/s", $xml, $contents);
- var_dump($k[1]);
- var_dump($contents);
- exit;
- */
- foreach (end($k) as $key => $val) {
- preg_match("/<$val>(.*?)<\/$val>/s", $xml, $arr[]);
- }
- foreach ($arr as $key1 => & $val1) {
- $val1 = end($val1);
- }
- //$arrkey = preg_match_all("/<\w+>/", $xml ,$k);
- //$arrval = preg_match_all("/>\w*(.*?)<\//s", $xml ,$v);
- //$arrval = preg_match_all("/>(.*?)<\//s", $xml ,$v);
- //$arrkey = preg_split('/[(^<\W+>*<\/\W+>$)]+/', $xml);
- //return $arr;
- $arr = array_combine(end($k), $arr);
- return $arr;
- }
- function test($arr)
- {
- }
- //var_dump(readxml($str));