Is there a way to get phpinfo () to output its data without formatting. How is it in CLI mode?

But of course, in normal mode, not the CLI. Generated output, included in other HTML, destroys the existing layout of the web page.

+7
source share
3 answers

There are some good examples of converting information into an array on PHP.net.

Here is a better example . You can skip this array to display it in any way.

+2
source

This function does a great job of converting phpinfo to an array.

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
source

I just finished creating a composer library for this purpose. Right now, it can analyze the output of phpinfo () when called from the command line, which was my use case.

Instead of using strip_tags () or any smart trick, I just worked my way back from everything the original function did.

You can use the library like this:

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

You can access keys in modules and in other places:

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

or

 echo $phpInfoArray['Thread Safety'] # Will output 'disabled' if disabled. 
+1
source

All Articles