note 90624 added to function.parse-ini-file
| From: | jeremygiberson at gmail dot com | Date: | Thu, 30 Apr 2009 23:01:16 +0000 |
| Subject: | note 90624 added to function.parse-ini-file | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-154064@lists.php.net to get a copy of this message | ||
Here is a quick parse_ini_file wrapper to add extend support to save typing and redundancy.
<?php
/**
* Parses INI file adding extends functionality via ":base" postfix on namespace.
*
* @param string $filename
* @return array
*/
function parse_ini_file_extended($filename) {
$p_ini = parse_ini_file($filename, true);
$config = array();
foreach($p_ini as $namespace => $properties){
list($name, $extends) = explode(':', $namespace);
$name = trim($name);
$extends = trim($extends);
// create namespace if necessary
if(!isset($config[$name])) $config[$name] = array();
// inherit base namespace
if(isset($p_ini[$extends])){
foreach($p_ini[$extends] as $prop => $val)
$config[$name][$prop] = $val;
}
// overwrite / set current namespace values
foreach($properties as $prop => $val)
$config[$name][$prop] = $val;
}
return $config;
}
?>
Treats this ini:
<?php
/*
[base]
host=localhost
user=testuser
pass=testpass
database=default
[users:base]
database=users
[archive : base]
database=archive
*/
?>
As if it were like this:
<?php
/*
[base]
host=localhost
user=testuser
pass=testpass
database=default
[users:base]
host=localhost
user=testuser
pass=testpass
database=users
[archive : base]
host=localhost
user=testuser
pass=testpass
database=archive
*/
?>
----
Server IP: 208.69.120.35
Probable Submitter: 65.121.168.50
----
Manual Page -- http://www.php.net/manual/en/function.parse-ini-file.php
Edit -- https://master.php.net/note/edit/90624
Del: integrated -- https://master.php.net/note/delete/90624/integrated
Del: useless -- https://master.php.net/note/delete/90624/useless
Del: bad code -- https://master.php.net/note/delete/90624/bad+code
Del: spam -- https://master.php.net/note/delete/90624/spam
Del: non-english -- https://master.php.net/note/delete/90624/non-english
Del: in docs -- https://master.php.net/note/delete/90624/in+docs
Del: other reasons-- https://master.php.net/note/delete/90624
Reject -- https://master.php.net/note/reject/90624
Search -- https://master.php.net/manage/user-notes.php