2014-02-13 77 views
0

有沒有辦法做到這一點?如何獲得phpinfo()的輸出爲xml?

我發現這個功能:

function phpinfo_array($return=true){ 
      /* Andale! Andale! Yee-Hah! */ 
      ob_start(); 
      phpinfo(-1); 

      $pi = preg_replace(
      array('#^.*<body>(.*)</body>.*$#ms', '#<h2>PHP License</h2>.*$#ms', 
      '#<h1>Configuration</h1>#', "#\r?\n#", "#</(h1|h2|h3|tr)>#", '# +<#', 
      "#[ \t]+#", '#&nbsp;#', '# +#', '# class=".*?"#', '%&#039;%', 
       '#<tr>(?:.*?)" src="(?:.*?)=(.*?)" alt="PHP Logo" /></a>' 
       .'<h1>PHP Version (.*?)</h1>(?:\n+?)</td></tr>#', 
       '#<h1><a href="(?:.*?)\?=(.*?)">PHP Credits</a></h1>#', 
       '#<tr>(?:.*?)" src="(?:.*?)=(.*?)"(?:.*?)Zend Engine (.*?),(?:.*?)</tr>#', 
       "# +#", '#<tr>#', '#</tr>#'), 
      array('$1', '', '', '', '</$1>' . "\n", '<', ' ', ' ', ' ', '', ' ', 
       '<h2>PHP Configuration</h2>'."\n".'<tr><td>PHP Version</td><td>$2</td></tr>'. 
       "\n".'<tr><td>PHP Egg</td><td>$1</td></tr>', 
       '<tr><td>PHP Credits Egg</td><td>$1</td></tr>', 
       '<tr><td>Zend Engine</td><td>$2</td></tr>' . "\n" . 
       '<tr><td>Zend Egg</td><td>$1</td></tr>', ' ', '%S%', '%E%'), 
      ob_get_clean()); 

      $sections = explode('<h2>', strip_tags($pi, '<h2><th><td>')); 
      unset($sections[0]); 

      $pi = array(); 
      foreach($sections as $section){ 
       $n = substr($section, 0, strpos($section, '</h2>')); 
       preg_match_all(
       '#%S%(?:<td>(.*?)</td>)?(?:<td>(.*?)</td>)?(?:<td>(.*?)</td>)?%E%#', 
       $section, $askapache, PREG_SET_ORDER); 
       foreach($askapache as $m) 
        $pi[$n][$m[1]]=(!isset($m[3])||@$m[2]==$m[3])[email protected]$m[2]:array_slice($m,2); 
      } 

      return ($return === false) ? print_r($pi) : $pi; 

}

該函數返回與來自的phpinfo看起來相當像錘方法的數據()的陣列;)

但似乎,就像phpinfo()提供的數據一樣,不能實現其他功能。

現在我怎樣才能正確地將這個複雜的數組變成有效的XML?

+3

你發現了一個功能,就是這樣嗎?你的問題是什麼?當你嘗試時發生了什麼?你得到不正確的結果了嗎?你有沒有得到結果?什麼不行?請解釋。 –

+0

我認爲HTML DOM解析器可以實現這一點(將HTML導出爲XML)。 – gskema

回答

0

它看起來像你的函數將返回一個數組。

通過遍歷數組並添加鍵和值,可以使用SimpleXMLElement輕鬆地將其轉換爲xml。

有一些很好的答案here