note 75355 deleted from function.parse-ini-file by nlopess

From: Date: Sun, 27 May 2007 19:03:37 +0000
Subject: note 75355 deleted from function.parse-ini-file by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-126970@lists.php.net to get a copy of this message
Note Submitter: Mildred ---- I wrote few functions to work with ini files. The function make_ini_file($array, &$errors) The function read_ini($file) The function prepare_ini($array, $maxdepth=NULL) The function read_ini($file) it adds functionnality to ini files. You can use arbitrary deep structure simlply by putting dots (.) in names (section names or key names). if a section name is "." (and nothing else than a dot) it will be as if the keys following were written at the beginning of the file (that is before any section). If a key name is "." (a single dot) it would be as if the key was written at the beginning of the file and the name of the key were exactly the name in the section it apears on. If a name begin or end with a dot, or if there two dots in a name, it will create a node whose name is the empty string ("") <?php function read_ini($file){ $ini = parse_ini_file($file, true); $res = array(); foreach($ini as $header=>$content){ if($header=='.') $header_parts = array(); else $header_parts = explode('.', $header); $current = &$res; foreach($header_parts as $part){ if(ereg('^[0-9]+$', $part)) $part = (int) $part; if(!isset($current[$part])) $current[$part] = array(); $current =& $current[$part]; } foreach($content as $key=>$value){ if($key=='.') $key_parts = array(); else $key_parts = explode('.', $key); $lastpart = array_pop($key_parts); $current2 = &$current; foreach($key_parts as $part){ if(ereg('^[0-9]+$', $part)) $part = (int) $part; if(!isset($current2[$part])){ $current2[$part] = array(); }elseif(!is_array($current2[$part])){ $current2[''] = $current2; $current2[$part] = array(); } $current2 =& $current2[$part]; } $current2[$lastpart] = $value; } } return $res; } // kate: indent-width 4; tab-width 8; space-indent on; // kate: replace-tabs off; remove-trailing-space on; ?>

« previous php.notes (#126970) next »