2011-05-17 15 views

Respuesta

2

Hay algunos buenos ejemplos de convertir la información en una matriz en el sitio PHP.net.

Here is the best example. Podría recorrer esa matriz para mostrarla de la forma que desee.

1

Este function hace un gran trabajo al convertir phpinfo en una matriz.

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

Acabo de terminar de crear el composer library para este propósito. En este momento, puede analizar el resultado de phpinfo() cuando se invoca desde la línea de comando, que era mi caso de uso.

En lugar de usar strip_tags() o cualquier truco inteligente, simplemente trabajé hacia atrás desde todo lo que hizo el original function.

Puede utilizar la biblioteca de este modo:

<?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()); 
?> 

Puede acceder a las claves dentro de los módulos y en otros lugares:

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

o

echo $phpInfoArray['Thread Safety'] # Will output 'disabled' if disabled. 
Cuestiones relacionadas