note 118151 added to function.parse-ini-file
| From: | jbricci at ya-right dot com | Date: | Thu, 15 Oct 2015 06:39:34 +0000 |
| Subject: | note 118151 added to function.parse-ini-file | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-203959@lists.php.net to get a copy of this message | ||
This core function won't handle ini key[][] = value(s), (multidimensional arrays), so if you
need to support that kind of setup you will need to write your own function. one way to do it is to
convert all the key = value(s) to array string [key][][]=value(s), then use parse_str() to convert
all those [key][][]=value(s) that way you just read the ini file line by line, instead of doing
crazy foreach() loops to handle those (multidimensional arrays) in each section, example...
ini file...... config.php
<?php
; This is a sample configuration file
; Comments start with ';', as in php.ini
[first_section]
one = 1
five = 5
animal = BIRD
[second_section]
path = "/usr/local/bin"
URL = "http://www.example.com/~username"
[third_section]
phpversion[] = "5.0"
phpversion[] = "5.1"
phpversion[] = "5.2"
phpversion[] = "5.3"
urls[svn] = "http://svn.php.net"
urls[git] = "http://git.php.net"
[fourth_section]
a[][][] = b
a[][][][] = c
a[test_test][][] = d
test[one][two][three] = true
?>
echo parse_ini_file ( "C:\\services\\www\\docs\\config.php" );
results in...
// PHP Warning: syntax error, unexpected TC_SECTION, expecting '=' line 27 -> a[][][]
= b
Here it simple function that handles (multidimensional arrays) without looping each key[][]=
value(s)
<?php
function getIni ( $file, $sections = FALSE )
{
$return = array ();
$keeper = array ();
$config = fopen ( $file, 'r' );
while ( ! feof ( $config ) )
{
$line = trim ( fgets ( $config, 1024 ) );
$line = ( $line == '' ) ? ' ' : $line;
switch ( $line{0} )
{
case ' ':
case '#':
case '/':
case ';':
case '<':
case '?':
break;
case '[':
if ( $sections )
{
$header = 'config[' . trim ( substr ( $line, 1, -1 ) ) . ']';
}
else
{
$header = 'config';
}
break;
default:
$kv = array_map ( 'trim', explode ( '=', $line ) );
$kv[0] = str_replace ( ' ', '+', $kv[0] );
$kv[1] = str_replace ( ' ', '+', $kv[1] );
if ( ( $pos = strpos ( $kv[0], '[' ) ) !== FALSE )
{
$kv[0] = '[' . substr ( $kv[0], 0, $pos ) . ']' . substr ( $kv[0], $pos );
}
else
{
$kv[0] = '[' . $kv[0] . ']';
}
$bt = strtolower ( $kv[1] );
if ( in_array ( $bt, array ( 'true', 'false', 'on', 'off'
) ) )
{
$kv[1] = ( $bt == 'true' || $bt == 'on' ) ? TRUE : FALSE;
}
$keeper[] = $header . $kv[0] . '=' . $kv[1];
}
}
fclose ( $config );
parse_str ( implode ( '&', $keeper ), $return );
return $return['config'];
}
// usage...
$sections = TRUE;
print_r ( $config->getIni ( "C:\\services\\www\\docs\\config.php" ), $sections );
?>
----
Server IP: 72.52.91.14
Probable Submitter: 24.60.50.89
----
Manual Page -- http://php.net/manual/en/function.parse-ini-file.php
Edit -- https://master.php.net/note/edit/118151
Del: integrated -- https://master.php.net/note/delete/118151/integrated
Del: useless -- https://master.php.net/note/delete/118151/useless
Del: bad code -- https://master.php.net/note/delete/118151/bad+code
Del: spam -- https://master.php.net/note/delete/118151/spam
Del: non-english -- https://master.php.net/note/delete/118151/non-english
Del: in docs -- https://master.php.net/note/delete/118151/in+docs
Del: other reasons-- https://master.php.net/note/delete/118151
Reject -- https://master.php.net/note/reject/118151
Search -- https://master.php.net/manage/user-notes.php