note 23683 deleted from function.parse-ini-file by victor
| From: | victor@php.net | Date: | Sat, 19 Jun 2004 15:19:49 +0000 |
| Subject: | note 23683 deleted from function.parse-ini-file by victor | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-71357@lists.php.net to get a copy of this message | ||
Note Submitter: seb@cine7.net
----
Here is a "better" function which should work better than the php one (especially the
double quotes problem). It's base on a previous piece of code submitted here, I've just
transformed it into a function.
/**
* array better_parse_ini_file (string $filename [, boolean $process_sections] )
*
* Purpose: Load in the ini file specified in filename, and return
* the settings in an associative array. By setting the
* last $process_sections parameter to true, you get a
* multidimensional array, with the section names and
* settings included. The default for process_sections is
* false.
*
* Return: - An associative array containing the data
* - false if any error occured
*
* Author: Sebastien Cevey <seb@cine7.net>
* Original Code base : <info@megaman.nl>
*/
function better_parse_ini_file($filename, $process_sections = false) {
$ini_array = array();
$sec_name = "";
$lines = file($filename);
foreach($lines as $line) {
$line = trim($line);
if($line == "") {
continue;
}
if($line[0] == "[" && $line[strlen($line) - 1] == "]") {
$sec_name = substr($line, 1, strlen($line) - 2);
}
else {
$pos = strpos($line, "=");
$property = substr($line, 0, $pos);
$value = substr($line, $pos + 1);
if($process_sections) {
$ini_array[$sec_name][$property] = $value;
}
else {
$ini_array[$property] = $value;
}
}
}
return $ini_array;
}
Hope it helps :)