note 62052 added to function.file
| From: | ilion at createcnia dot com | Date: | Sat, 18 Feb 2006 12:30:17 +0000 |
| Subject: | note 62052 added to function.file | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-103965@lists.php.net to get a copy of this message | ||
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 ;-)
----
Server IP: 130.206.1.5
Probable Submitter: 85.49.12.110
----
Manual Page -- http://www.php.net/manual/en/function.file.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+62052
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+62052&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+62052&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+62052&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+62052&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+62052&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+62052&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+62052&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+62052&report=yes
Search -- http://master.php.net/manage/user-notes.php