note 51122 deleted from function.file by bjori
| From: | bjori@php.net | Date: | Wed, 22 Mar 2006 13:47:57 +0000 |
| Subject: | note 51122 deleted from function.file by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-106251@lists.php.net to get a copy of this message | ||
Note Submitter: camilokawerin(at)ciudad(dot)com(dot)ar
----
This function allows to read configuration files like this:
[level_0]
total=>8
[level_1]
1=3
[/level_1]
[level_1]
[level_2]
1=something
[/level_2]
[/level_1]
[/level_0]
where 'level_n' may be replaced by any label to create a tree structure. This is usefull
to fill an array with all the values you need in one script.
$archivo="configuration.txt";
$resultado=file($archivo);
reset($resultado);
$n=0;
foreach ($resultado as $linea) {
if (ereg("^\[\/.*\]$", rtrim($linea))) { //close labels
$n=$n-1;
}
elseif (ereg("^\[.*\]$", trim($linea), $nombre)) { //open labels
$n+=1;
$nivel[$n]=ereg_replace("(\[|\])", "", $nombre[0]);
}
elseif (($linea!="") and ($linea!="\n") and !ereg("(\/\*|\*\/)",
$linea)) { //ignore empty lines and comments in '/* */' format
$pares=explode("=", trim($linea)); //separate key and value
for($i=$n; $i>0; $i--) { //fill the array
if ($i==$n) {${$nivel[$i]}[$pares[0]]=$pares[1]; }
else {${$nivel[$i]}[$nivel[$i+1]]=${$nivel[$i+1]}; }
}
$propiedades[$nivel[1]]=${$nivel[1]};
}
}