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 — 設定文字列をパースする
説明
array parse_ini_string
( string $ini
[, bool $process_sections = false
[, int $scanner_mode = INI_SCANNER_NORMAL
]] )
parse_ini_string() は、文字列 ini の設定を連想配列で返します。
文字列の構造は、php.ini の構造と同じです。
パラメータ
- ini
-
パースしたい ini ファイルの内容。
- process_sections
-
process_sections を TRUE に設定すると、セクション名と設定を含む多次元配列を返します。 process_sections のデフォルトは FALSE です。
- scanner_mode
-
INI_SCANNER_NORMAL (デフォルト) あるいは INI_SCANNER_RAW。INI_SCANNER_RAW を指定すると、オプションの値はパースされません。
返り値
成功した場合に設定を連想配列形式で返します。 失敗した場合に FALSE を返します。
注意
注意: ini ファイル上でキーとして使ってはいけない単語があります。 null, yes, no, true, false, on, off, none などです。 null, no および false は "" となり、yes および true は "1" となります。 次の文字 {}|&~