note 51122 added to function.file

From: Date: Mon, 21 Mar 2005 00:46:26 +0000
Subject: note 51122 added to function.file
Groups: php.notes 
Request: Send a blank email to php-notes+get-86725@lists.php.net to get a copy of this message
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]}; } } ---- Manual Page -- http://www.php.net/manual/en/function.file.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+51122 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+51122&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+51122&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+51122&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+51122&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+51122&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+51122&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#86725) next »