我想要一个简单的 *函数* 将 phpinfo 的输出转换为数组。 这是我得益于之前很多作者的提示和源文件:php-5.2.6/ext/standard/info.c 得出的结果。
像 phpinfo_array() 这样调用此函数将打印数组,phpinfo_array(1) 将返回数组以供您自己处理。
== 示例输出 ==
[PHP 配置] => 数组
(
[PHP 版本] => 5.2.6
[PHP Egg] => PHPE9568F34-D428-11d2-A769-00AA001ACF42
[系统] => Linux askapache 2.6.22.19-grsec3
[构建日期] => 2008 年 11 月 11 日 13:09:07
[配置命令] => ./configure --prefix=/home/grsec/bin/php
[服务器 API] => FastCGI
[IPv6 支持] => 已启用
[Zend Egg] => PHPE9568F35-D428-11d2-A769-00AA001ACF42
[PHP Credits Egg] => PHPB8B5F2A0-3C92-11d3-A3A9-4C7B08C10000
)
[mbstring] => 数组
(
[mbstring.http_input] => pass
[mbstring.internal_encoding] => 数组
(
[0] => ISO-8859-1
[1] => no value
)
[mbstring.language] => neutral
)
[mcrypt] => 数组
(
[版本] => 3.5.7
[Api No] => 20031217
)
<?php
function phpinfo_array($return=false){
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]+#", '# #', '# +#', '# class=".*?"#', '%'%',
'#<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])?$m[2]:array_slice($m,2);
}
return ($return === false) ? print_r($pi) : $pi;
}
?>