note 72838 deleted from function.parse-ini-file by danbrown
| From: | danbrown@php.net | Date: | Sun, 23 Jan 2011 17:11:42 +0000 |
| Subject: | note 72838 deleted from function.parse-ini-file by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-176159@lists.php.net to get a copy of this message | ||
Note Submitter: mhall at lakeland dot net
----
I modified phpcoder's readINIFile function to allow multi-lined values. Adding a backslash (\)
to the end of a line indicates that the whole of the next line should be appended to the value.
Leading whitespace is ignored on continues lines, whitespace before the backslash is preserved. This
is the same as the Java Properties spec: http://java.sun.com/j2se/1.4.2/docs/api/java/util/Properties.html
<?php
function readINIfile ($filename, $commentchar) {
$array1 = file($filename);
$section = '';
for ($line_num = 0; $line_num <= sizeof($array1); $line_num++) {
$filedata = $array1[$line_num];
$dataline = trim($filedata);
$firstchar = substr($dataline, 0, 1);
if ($firstchar!=$commentchar && $dataline!='') {
//It's an entry (not a comment and not a blank line)
if ($firstchar == '[' && substr($dataline, -1, 1) == ']') {
//It's a section
$section = strtolower(substr($dataline, 1, -1));
}else{
//It's a key...
$delimiter = strpos($dataline, '=');
if ($delimiter > 0) {
//...with a value
$key = strtolower(trim(substr($dataline, 0, $delimiter)));
$array2[$section][$key] = '';
$value = trim(substr($dataline, $delimiter + 1));
while (substr($value, -1, 1) == '\\') {
//...value continues on the next line
$value = substr($value, 0, strlen($value)-1);
$array2[$section][$key] .= stripcslashes($value);
$line_num++;
$value = trim($array1[$line_num]);
}
$array2[$section][$key] .= stripcslashes($value);
$array2[$section][$key] = trim($array2[$section][$key]);
if (substr($array2[$section][$key], 0, 1) == '"' &&
substr($array2[$section][$key], -1, 1) == '"') {
$array2[$section][$key] = substr($array2[$section][$key], 1, -1);
}
}else{
//...without a value
$array2[$section][strtolower(trim($dataline))]='';
}
}
}else{
//It's a comment or blank line. Ignore.
}
}
return $array2;
}
?>