note 82900 added to function.parse-ini-file
| From: | asohn~at~aircanopy~dot~net at osu1 dot php dot net | Date: | Wed, 30 Apr 2008 22:27:39 +0000 |
| Subject: | note 82900 added to function.parse-ini-file | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-138950@lists.php.net to get a copy of this message | ||
Comments don't have to have an entire line dedicated to them. You can put a comment on the same
line as a section or variable/value declaration and the built-in parse_ini_file() function will omit
them. This being the case I took the liberty of revising goulven.ch AT gmail DOT com 's
parse_ini() function. I also added the $process_sections argument to better reflect PHP's
built-in parse_ini_file(). As soon as a semicolon is found in a line everything from that position
to the end of the line is omitted so as to not become part of the value. However, any semicolon
found that occurs between a single-quote or double-quote will be left alone to become part of the
value.
<?php
function _parse_ini_file($file, $process_sections = false) {
$process_sections = ($process_sections !== true) ? false : true;
$ini = file($file);
if (count($ini) == 0) {return array();}
$sections = array();
$values = array();
$result = array();
$globals = array();
$i = 0;
foreach ($ini as $line) {
$line = trim($line);
$line = str_replace("\t", " ", $line);
// Comments
if (!preg_match('/^[a-zA-Z0-9[]/', $line)) {continue;}
// Sections
if ($line{0} == '[') {
$tmp = explode(']', $line);
$sections[] = trim(substr($tmp[0], 1));
$i++;
continue;
}
// Key-value pair
list($key, $value) = explode('=', $line, 2);
$key = trim($key);
$value = trim($value);
if (strstr($value, ";")) {
$tmp = explode(';', $value);
if (count($tmp) == 2) {
if ((($value{0} != '"') && ($value{0} != "'")) ||
preg_match('/^".*"\s*;/', $value) ||
preg_match('/^".*;[^"]*$/', $value) ||
preg_match("/^'.*'\s*;/", $value) ||
preg_match("/^'.*;[^']*$/", $value) ){
$value = $tmp[0];
}
} else {
if ($value{0} == '"') {
$value = preg_replace('/^"(.*)".*/', '$1', $value);
} elseif ($value{0} == "'") {
$value = preg_replace("/^'(.*)'.*/", '$1', $value);
} else {
$value = $tmp[0];
}
}
}
$value = trim($value);
$value = trim($value, "'\"");
if ($i == 0) {
if (substr($line, -1, 2) == '[]') {
$globals[$key][] = $value;
} else {
$globals[$key] = $value;
}
} else {
if (substr($line, -1, 2) == '[]') {
$values[$i-1][$key][] = $value;
} else {
$values[$i-1][$key] = $value;
}
}
}
for($j = 0; $j < $i; $j++) {
if ($process_sections === true) {
$result[$sections[$j]] = $values[$j];
} else {
$result[] = $values[$j];
}
}
return $result + $globals;
}
?>
usage regarding semicolons:
<?php
;sample.ini
variable1 = v1;v1
variable 2 = "v2;v2"
variable_3 = "v3;v3;v3"
variable4 = "v4;v4" ;v4
variable 5 = "v5;v5;v5" ;v5
variable_6 = "v6;v6" ;v6;;
variable7 = "v7;;v7"
variable 8 = 'v8;v8'
variable_9 = 'v9;v9;v9'
variable10 = 'v10;v10' ;v10
variable 11 = 'v11;v11;v11' ;v11
variable_12 = 'v12;v12' ;v2;;
variable13 = 'v13;;v13'
variable 14 = "v14
variable_15 = 'v15
variable16 = "v16;v16
variable 17 = 'v17;v17
?>
<?php
//example.php
print_r(_parse_ini_file("sample.ini"));
?>
<?php
//example.php output
Array
(
[variable1] => v1
[variable 2] => v2;v2
[variable_3] => v3;v3;v3
[variable4] => v4;v4
[variable 5] => v5;v5;v5
[variable_6] => v6;v6
[variable7] => v7;;v7
[variable 8] => v8;v8
[variable_9] => v9;v9;v9
[variable10] => v10;v10
[variable 11] => v11;v11;v11
[variable_12] => v12;v12
[variable13] => v13;;v13
[variable 14] => v14
[variable_15] => v15
[variable16] => v16
[variable 17] => v17
)
?>
----
Server IP: 209.41.74.194
Probable Submitter: 66.160.208.254
----
Manual Page -- http://www.php.net/manual/en/function.parse-ini-file.php
Edit -- https://master.php.net/note/edit/82900
Del: integrated -- https://master.php.net/note/delete/82900/integrated
Del: useless -- https://master.php.net/note/delete/82900/useless
Del: bad code -- https://master.php.net/note/delete/82900/bad+code
Del: spam -- https://master.php.net/note/delete/82900/spam
Del: non-english -- https://master.php.net/note/delete/82900/non-english
Del: in docs -- https://master.php.net/note/delete/82900/in+docs
Del: other reasons-- https://master.php.net/note/delete/82900
Reject -- https://master.php.net/note/reject/82900
Search -- https://master.php.net/manage/user-notes.php