PHP头条
热点:

PHP XML转换为数组函数


PHP XML转换为数组函数

  1. <?php
  2.  function xml2array($originalXML, $attributes=true)
  3.  {
  4.          $xmlArray = array();
  5.          $search = $attributes ? |<((S )(.*))s*>(.*)</<?php
     function xml2array($originalXML, $attributes=true)
     {
             $xmlArray = array();
             $search = $attributes ? |<((S )(.*))s*>(.*)</2>|Ums : |<((S )()).*>(.*)</2>|Ums;
           
             // normalize data
             $xml = preg_replace(|>s*<|, "> <", $originalXML); // one tag per line
             $xml = preg_replace(|<?.*?>|, , $xml);            // remove XML declarations
             $xml = preg_replace(|<(S ?)(.*)/>|U, <$1$2></$1>, $xml); //Expand singletons
           
             if (! preg_match_all($search, $xml, $xmlMatches))
                     return trim($originalXML);      // bail out - no XML found
                   
             foreach ($xmlMatches[1] as $index => $key)
             {
                     if (! isset($xmlArray[$key])) $xmlArray[$key] = array();      
                     $xmlArray[$key][] = xml2array($xmlMatches[4][$index], $attributes);
             }
             return $xmlArray;
     }
     ?>>|Ums : |<((S )()).*>(.*)</<?php
     function xml2array($originalXML, $attributes=true)
     {
             $xmlArray = array();
             $search = $attributes ? |<((S )(.*))s*>(.*)</2>|Ums : |<((S )()).*>(.*)</2>|Ums;
           
             // normalize data
             $xml = preg_replace(|>s*<|, "> <", $originalXML); // one tag per line
             $xml = preg_replace(|<?.*?>|, , $xml);            // remove XML declarations
             $xml = preg_replace(|<(S ?)(.*)/>|U, <$1$2></$1>, $xml); //Expand singletons
           
             if (! preg_match_all($search, $xml, $xmlMatches))
                     return trim($originalXML);      // bail out - no XML found
                   
             foreach ($xmlMatches[1] as $index => $key)
             {
                     if (! isset($xmlArray[$key])) $xmlArray[$key] = array();      
                     $xmlArray[$key][] = xml2array($xmlMatches[4][$index], $attributes);
             }
             return $xmlArray;
     }
     ?>>|Ums;
  6.        
  7.          // normalize data
  8.          $xml = preg_replace(|>s*<|, "> <", $originalXML); // one tag per line
  9.          $xml = preg_replace(|<?.*?>|, , $xml);            // remove XML declarations
  10.          $xml = preg_replace(|<(S ?)(.*)/>|U, <[code]<?php
     function xml2array($originalXML, $attributes=true)
     {
             $xmlArray = array();
             $search = $attributes ? |<((S )(.*))s*>(.*)</2>|Ums : |<((S )()).*>(.*)</2>|Ums;
           
             // normalize data
             $xml = preg_replace(|>s*<|, "> <", $originalXML); // one tag per line
             $xml = preg_replace(|<?.*?>|, , $xml);            // remove XML declarations
             $xml = preg_replace(|<(S ?)(.*)/>|U, <$1$2></$1>, $xml); //Expand singletons
           
             if (! preg_match_all($search, $xml, $xmlMatches))
                     return trim($originalXML);      // bail out - no XML found
                   
             foreach ($xmlMatches[1] as $index => $key)
             {
                     if (! isset($xmlArray[$key])) $xmlArray[$key] = array();      
                     $xmlArray[$key][] = xml2array($xmlMatches[4][$index], $attributes);
             }
             return $xmlArray;
     }
     ?>></[code]>, $xml); //Expand singletons
  11.        
  12.          if (! preg_match_all($search, $xml, $xmlMatches))
  13.                  return trim($originalXML);      // bail out - no XML found
  14.                
  15.          foreach ($xmlMatches[1] as $index => $key)
  16.          {
  17.                  if (! isset($xmlArray[$key])) $xmlArray[$key] = array();      
  18.                  $xmlArray[$key][] = xml2array($xmlMatches[4][$index], $attributes);
  19.    &nb

    www.phpzy.comtrue/phprm/25007.htmlTechArticlePHP XML转换为数组函数 PHP XML转换为数组函数 ?php function xml2array($originalXML, $attributes=true) { $xmlArray = array(); $search = $attributes ? |((S )(.*))s*(.*)/?php function xml2array($originalXML, $attributes=true) { $xm...

相关文章

    暂无相关文章

PHP之友评论

今天推荐