Parsing INI Files


 (parse_ini_file('php.ini', true) 


Another file format that is very well-known is the INI file formatit was very widely used in the Windows world, but today also drives the configuration of complex software products like PHP. For instance, take a look at php.ini. Here is a (modified) excerpt from the default php.ini that shows very well the structure of INI files:

 [mail function] ; For Win32 only. ; SMTP = localhost ; For Unix only.  You may supply arguments as well    (default: "sendmail -t -i"). sendmail_path = 

So, there are sections that start with a section headline in square brackets, comments that start with a semicolon, and settings in the format name=value. The PHP function parse_ini_file() now reads in such a PHP file and creates an array out of it: Each section is a subarray, and within those subarrays you find the names and the values of directives in the INI file. The code at the beginning of this phrase does this (remember to change the path to php.ini, if appropriate), and Figure 6.4 shows the output.

Reading Information from INI Files (parse_ini_file.php)
 <?php   echo '<xmp>' .     print_r(parse_ini_file('php.ini', true), true) .     '</xmp>'; ?> 

Figure 6.4. The contents of php.ini as a multidimensional array (parse_ini_file.php; excerpt).


TIP

If you want to avoid the grouping by sections in the INI file, just omit the second parameter parse_ini_file(); then, you get all settings at once.

 parse_ini_file('php.ini'); 





PHP Phrasebook
PHP Phrasebook
ISBN: 0672328178
EAN: 2147483647
Year: 2005
Pages: 193

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net