note 58368 added to function.file
| From: | comdotgmailattrucex | Date: | Wed, 02 Nov 2005 01:37:53 +0000 |
| Subject: | note 58368 added to function.file | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-97957@lists.php.net to get a copy of this message | ||
I used the config file reader written by mvanbeek at supporting-role dot co dot uk for one of my own
scripts. I have made some modifications, and turned it into a class.
<?
class config_reader {
var $filename;
var $databaseHost;
var $databaseName;
var $databaseUser;
var $databasePassword;
function config_reader () {
$this->filename = './settings.conf';
$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,"#") == 1) {
next($config);
} else {
list($key, $value) = preg_split("/\s*=\s*/", $line, 2); # split by "="
and removing blank space either side of it.
$this->$key = $value;
}
}
}
}
?>
Changes:
- I have cleaned up the layout of the code for readability and faster execution.
- I have moved the rtrim and ltrim functions to execute before the line check, to make sure the
line wasn't just a space, or a comment after a space.
- I changed strstr($line, "#") to strstr($line, "#") == 1 to make sure that
lines with a comment after the values are not left behind.
- I also changed the way preg_split assigns the results, primarily to clean up the code.
- I changed the way values are assigned to the variables. For me, this works better because I only
needed to get the database information from the user. If you would like a dynamic listing of
variables, do the following:
Remove the following lines:
<?
var $databaseHost;
var $databaseName;
var $databaseUser;
var $databasePassword;
?>
Add this line: (commented lines are only to provide location for new line)
<?
#class config_reader {
#var $filename;
var $settings; #ADD THIS LINE
?>
Then change this line:
<?
$this->$key = $value;
?>
to this:
<?
$this->settings[$key] = $value;
?>
NOTE: This has not actually been executed, so their might be a small error. I don't recommend
use of this script by those new to PHP, as you will need an understanding of file permissions and
setting the variables in classes.
----
Manual Page -- http://www.php.net/manual/en/function.file.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+58368
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+58368&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+58368&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+58368&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+58368&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+58368&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+58368&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+58368&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+58368&report=yes
Search -- http://master.php.net/manage/user-notes.php