note 117961 added to function.phpinfo

From: Date: Wed, 09 Sep 2015 12:38:23 +0000
Subject: note 117961 added to function.phpinfo
Groups: php.notes 
Request: Send a blank email to php-notes+get-203750@lists.php.net to get a copy of this message
After reading and trying various functions, I couldn't find one that correctly parses all the configurations, strips any left-over html tag and converts special characters into UTF8 (e.g. &#039; into '), so I created my own by improving on the existing ones: function phpinfo2array() { $entitiesToUtf8 = function($input) { // http://php.net/manual/en/function.html-entity-decode.php#104617 return preg_replace_callback("/(&#[0-9]+;)/", function($m) { return mb_convert_encoding($m[1], "UTF-8", "HTML-ENTITIES"); }, $input); }; $plainText = function($input) use ($entitiesToUtf8) { return trim(html_entity_decode($entitiesToUtf8(strip_tags($input)))); }; $titlePlainText = function($input) use ($plainText) { return '# '.$plainText($input); }; ob_start(); phpinfo(-1); $phpinfo = array('phpinfo' => array()); // Strip everything after the <h1>Configuration</h1> tag (other h1's) if (!preg_match('#(.*<h1[^>]*>\s*Configuration.*)<h1#s', ob_get_clean(), $matches)) { return array(); } $input = $matches[1]; $matches = array(); if(preg_match_all( '#(?:<h2.*?>(?:<a.*?>)?(.*?)(?:<\/a>)?<\/h2>)|'. '(?:<tr.*?><t[hd].*?>(.*?)\s*</t[hd]>(?:<t[hd].*?>(.*?)\s*</t[hd]>(?:<t[hd].*?>(.*?)\s*</t[hd]>)?)?</tr>)#s', $input, $matches, PREG_SET_ORDER )) { foreach ($matches as $match) { $fn = strpos($match[0], '<th') === false ? $plainText : $titlePlainText; if (strlen($match[1])) { $phpinfo[$match[1]] = array(); } elseif (isset($match[3])) { $keys1 = array_keys($phpinfo); $phpinfo[end($keys1)][$fn($match[2])] = isset($match[4]) ? array($fn($match[3]), $fn($match[4])) : $fn($match[3]); } else { $keys1 = array_keys($phpinfo); $phpinfo[end($keys1)][] = $fn($match[2]); } } } return $phpinfo; } The output looks something like this (note the headers are also included but are prefixed with '# ', e.g. '# Directive'): Array ( [phpinfo] => Array ( [0] => PHP Version 5.6.5 [System] => Darwin Calins-MBP 15.0.0 Darwin Kernel Version 15.0.0: Wed Aug 26 19:41:34 PDT 2015; root:xnu-3247.1.106~5/RELEASE_X86_64 x86_64 [Build Date] => Feb 19 2015 18:34:18 [Registered Stream Socket Transports] => tcp, udp, unix, udg, ssl, sslv3, sslv2, tls, tlsv1.0 [Registered Stream Filters] => zlib.*, bzip2.*, convert.iconv.*, string.rot13, string.toupper, string.tolower, string.strip_tags, convert.*, consumed, dechunk [1] => This program makes use of the Zend Scripting Language Engine:Zend Engine... ) [apache2handler] => Array ( [Apache Version] => Apache/2.4.16 (Unix) PHP/5.6.5 OpenSSL/0.9.8zg [Apache API Version] => 20120211 [Server Administrator] => webmaster@dummy-host2.example.com [Hostname:Port] => sitestacker.local:0 [# Directive] => Array ( [0] => # Local Value [1] => # Master Value ) [engine] => Array ( [0] => 1 [1] => 1 ) [last_modified] => Array ( [0] => 0 [1] => 0 ) ---- Server IP: 72.52.91.14 Probable Submitter: 79.116.208.12 ---- Manual Page -- http://php.net/manual/en/function.phpinfo.php Edit -- https://master.php.net/note/edit/117961 Del: integrated -- https://master.php.net/note/delete/117961/integrated Del: useless -- https://master.php.net/note/delete/117961/useless Del: bad code -- https://master.php.net/note/delete/117961/bad+code Del: spam -- https://master.php.net/note/delete/117961/spam Del: non-english -- https://master.php.net/note/delete/117961/non-english Del: in docs -- https://master.php.net/note/delete/117961/in+docs Del: other reasons-- https://master.php.net/note/delete/117961 Reject -- https://master.php.net/note/reject/117961 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#203750) next »