streamlining php.ini; php.ini is not parsable with parse_ini_file
| From: | D. Dante Lorenso | Date: | Fri, 26 May 2006 01:50:20 +0000 |
| Subject: | streamlining php.ini; php.ini is not parsable with parse_ini_file | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-236921@lists.php.net to get a copy of this message | ||
All,
I want to clean up my PHP.ini file on production to be as streamlined as possible so that it's easier to work with and maintain. It is easier to do a diff of my INI and the recommended INI if both files are very similar with whitespace removed, comments cleaned out, and keys sorted, etc. I've tried to use parse_ini_file() function to parse the INI file, but that won't work because the 'extension = *' key is used multiple times in the php.ini. parse_ini_file assumes unique assoc array is returned.
I'm guessing PHP doesn't actually use the parse_ini_file to read its own ini.
So, I then started parsing the ini myself with my own custom code and soon realized that PHP might not be using the [section] tags either as it uses the INI key=values. Seems like the [section] tags are used more like suggestions for key grouping than for anything functional. Is this a correct assumption? If so, I've got the following INI reading code which will munch the php.ini-recommended and output just the strings which are not commented out.
See here and let me know if this looks like robust code:
//----------------------------------------------------------------------
#!/usr/bin/php
<?php
// we pass the name of an INI file on the command line
$script = array_shift($argv);
$ini_file_name = array_shift($argv);
if (!$ini_file_name || !file_exists($ini_file_name)) {
die("usage: $script <php.ini file>\n");
}
// slurp in the whole ini file and init
$lines = file($ini_file_name);
$data = array();
foreach ($lines as $line) {
// remove trailing comments, newlines, and start/end whitespace
$line = trim(preg_replace("/;[^\"\']*$/", "", trim($line)));
// skip blank lines, commented lines, ini sections, or anything without an '=' in the line
if (!$line || $line{0} === ';' || $line{0} === '[' || strpos($line, '=') < 1) {
continue;} // fix spacing between equal sign list ($key, $value) = preg_split("/\s*=\s*/", $line, 2); $line = "$key = $value"; // save this key=value pair array_push($data, $line); } // sort and print all keys sort($data); print join("\n", $data); ?> //---------------------------------------------------------------------- Dante