Парсер конфигов
августа 10, 2007 by adminНаписал такой код:
INI - файл
; this is comment
key=val
[path]
param1=value1
param2=value2
param3=value3
[dirs]
runtime=/sys/rtm
data=/data
docs=/etc/docs
conf=conf.txt
[vars]
; asdads
array1[] = value1
array1[] = value2
array1[] = value3
hash2[abc] = value1
hash2[def] = value2
hash2[5] = value3
hash2[6] = value4
hash2[] = value5
hash2[] = value6
PHP Код
<?php
/**
* @author PEpeSto
* @license MPL 1.1
*/
$fileName = “config.ini”;
$iniArray =array();
$lines = file($fileName) or die(”Error: Can’t read file”);
$currentBlock=”";
foreach ($lines as $linenum=>$line)
{
// comments
if(preg_match(”/^[ \t]*;/”,$line)) continue;
// blocks
if(preg_match(”/^[ \t]*\[(.+?)\][ \t]*/”, $line, $matches)) $currentBlock = $matches[1];
// params
if(preg_match(”/^[ \t]*(.+?)[ \t]*=[ \t]*(.+)/”, $line, $matches))
if(preg_match(”/^(.+?)\[(.*)\]/”, $matches[1], $matches2))
($currentBlock!=”")
? (($matches2[2]!=”")
? $iniArray[$currentBlock][$matches2[1]][$matches2[2]] = $matches[2]
: $iniArray[$currentBlock][$matches2[1]][] = $matches[2])
: (($matches2[2]!=”")
? $iniArray[$matches2[1]][$matches2[2]] = $matches[2]
: $iniArray[$matches2[1]][] = $matches[2]);
else
($currentBlock!=”")
? $iniArray[$currentBlock][$matches[1]] = $matches[2]
: $iniArray[$matches[1]] = $matches[2];
}
print_r($iniArray);
?>
результат
Array
(
[key] => val
[path] => Array
(
[param1] => value1
[param2] => value2
[param3] => value3
)
[dirs] => Array
(
[runtime] => /sys/rtm
[data] => /data
[docs] => /etc/docs
[conf] => conf.txt
)
[vars] => Array
(
[array1] => Array
(
[0] => value1
[1] => value2
[2] => value3
)
[hash2] => Array
(
[abc] => value1
[def] => value2
[5] => value3
[6] => value4
[7] => value5
[8] => value6
)
)
)
мож пригодиться кому когда
потом как нибудь запись из массива в конфиг напишу
Posted in Без рубрики | No Comments »
