note 62052 deleted from function.file by bjori

From: Date: Wed, 22 Mar 2006 13:45:29 +0000
Subject: note 62052 deleted from function.file by bjori
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-106246@lists.php.net to get a copy of this message
Note Submitter: ilion at createcnia dot com ---- I used trucex modification of mvanbeek config file reader class, with the modifications trucex suggested to use generic keys. I added a couple of things to solve problems I had with comments. <? class config_reader { var $filename; var $settings; function config_reader ($myfile) { $this->filename = $myfile; $this->getSettings(); } function getSettings () { $config = file($this->filename); reset($config); foreach ($config as $line) { $line = rtrim($line); # Get rid of newline characters $line = ltrim($line); # Get rid of any leading spaces if ($line == "" || $line == "\n" || strstr($line,"#") === 0) { next($config); } else { list($key, $value) = preg_split("/\s*=\s*/", $line, 2); # split by "=" and removing blank space either side of it. # check for '#' coments after the value $pos=strpos($value,"#"); if($pos!==false) { $value = substr($value, 0, $pos); } $this->settings[$key] = $value; } } } } ?> Changes (in addition to trucex suggestions): - config file name as parameter in constructor - strstr($line,"#") === 0 instead of strstr($line,"#") == 1 - remove comments after a value. I am sure this can be done much better with regular expressions :-). Also would be a good idea to allow some escaping to allow the use of '#' inside values I tested it... but not to thoroughly. Use it at your own risk ;-)

« previous php.notes (#106246) next »