note 23683 added to function.parse-ini-file

From: Date: Wed, 24 Jul 2002 21:19:21 +0000
Subject: note 23683 added to function.parse-ini-file
Groups: php.notes 
Request: Send a blank email to php-notes+get-33526@lists.php.net to get a copy of this message
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 :) -- http://www.php.net/manual/en/function.parse-ini-file.php http://master.php.net/manage/user-notes.php?action=edit+23683 http://master.php.net/manage/user-notes.php?action=delete+23683 http://master.php.net/manage/user-notes.php?action=reject+23683

« previous php.notes (#33526) next »