If your server has yet not been updated to PHP 5.2 (like mine), the following function can help you without having to use temporary paths.
I tried to make it the more similar possible to the PHP's behaviour, including dealing with errors or exceptions.
<?php
if(!function_exists('parse_ini_string')){
function parse_ini_string($str, $ProcessSections=false){
$lines = explode("\n", $str);
$return = Array();
$inSect = false;
foreach($lines as $line){
$line = trim($line);
if(!$line || $line[0] == "#" || $line[0] == ";")
continue;
if($line[0] == "[" && $endIdx = strpos($line, "]")){
$inSect = substr($line, 1, $endIdx-1);
continue;
}
if(!strpos($line, '=')) // (We don't use "=== false" because value 0 is not valid as well)
continue;
$tmp = explode("=", $line, 2);
if($ProcessSections && $inSect)
$return[$inSect][trim($tmp[0])] = ltrim($tmp[1]);
else
$return[trim($tmp[0])] = ltrim($tmp[1]);
}
return $return;
}
}
?>
parse_ini_string
(PHP 5 >= 5.3.0)
parse_ini_string — Parse a configuration string
설명
parse_ini_string() returns the settings in string ini in an associative array.
The structure of the ini string is the same as the php.ini's.
인수
- ini
-
The contents of the ini file being parsed.
- process_sections
-
By setting the process_sections parameter to TRUE, you get a multidimensional array, with the section names and settings included. The default for process_sections is FALSE
- scanner_mode
-
Can either be INI_SCANNER_NORMAL (default) or INI_SCANNER_RAW. If INI_SCANNER_RAW is supplied, then option values will not be parsed.
반환값
The settings are returned as an associative array on success, and FALSE on failure.
주의
Note: There are reserved words which must not be used as keys for ini files. These include: null, yes, no, true, false, on, off, none. Values null, no and false results in "", yes and true results in "1". Characters {}|&~