2011-05-17 78 views

回答

2

在PHP.net網站上有一些將信息轉換爲數組的好例子。

Here is the best example。你可以循環訪問該數組,以任何你喜歡的方式顯示它。

1

這個function在將phpinfo轉換成數組方面做得非常出色。

function parse_phpinfo() { 
    ob_start(); phpinfo(INFO_MODULES); $s = ob_get_contents(); ob_end_clean(); 
    $s = strip_tags($s, '<h2><th><td>'); 
    $s = preg_replace('/<th[^>]*>([^<]+)<\/th>/', '<info>\1</info>', $s); 
    $s = preg_replace('/<td[^>]*>([^<]+)<\/td>/', '<info>\1</info>', $s); 
    $t = preg_split('/(<h2[^>]*>[^<]+<\/h2>)/', $s, -1, PREG_SPLIT_DELIM_CAPTURE); 
    $r = array(); $count = count($t); 
    $p1 = '<info>([^<]+)<\/info>'; 
    $p2 = '/'.$p1.'\s*'.$p1.'\s*'.$p1.'/'; 
    $p3 = '/'.$p1.'\s*'.$p1.'/'; 
    for ($i = 1; $i < $count; $i++) { 
     if (preg_match('/<h2[^>]*>([^<]+)<\/h2>/', $t[$i], $matchs)) { 
      $name = trim($matchs[1]); 
      $vals = explode("\n", $t[$i + 1]); 
      foreach ($vals AS $val) { 
       if (preg_match($p2, $val, $matchs)) { // 3cols 
        $r[$name][trim($matchs[1])] = array(trim($matchs[2]), trim($matchs[3])); 
       } elseif (preg_match($p3, $val, $matchs)) { // 2cols 
        $r[$name][trim($matchs[1])] = trim($matchs[2]); 
       } 
      } 
     } 
    } 
    return $r; 
} 
1

剛剛完成創建composer library這個目的。現在,它可以從命令行調用時解析phpinfo()輸出,這是我的用例。

而不是使用strip_tags()或任何聰明的把戲,我只是從original function所做的一切事情中倒退回去。

你可以使用這個庫就像這樣:

<?php 
include_once('vendor/autoload.php'); 

ob_start(); 
phpinfo(); 
$phpinfoAsString = ob_get_contents(); 
ob_get_clean(); 

$phpInfo = new OutCompute\PHPInfo\PHPInfo(); 
$phpInfo->setText($phpinfoAsString); 
var_export($phpInfo->get()); 
?> 

您可以模塊的訪問鍵和其他地方:

echo $phpInfoArray['Configuration']['bz2']['BZip2 Support']; # Will output 'Enabled' if enabled 

echo $phpInfoArray['Thread Safety'] # Will output 'disabled' if disabled. 
相關問題