PHP parse_ini_file() Function
Definition and Usage
The parse_ini_file() function parses a configuration file and returns the settings in the form of an array.
Syntax
parse_ini_file(file,process_sections)
Parameters | Description |
---|---|
file | Required. Specifies the ini file to be checked. |
process_sections | Optional. If set to true, it returns a multidimensional array including the names and settings of each section in the configuration file. The default is false. |
Description
The structure of the ini file is similar to that of php.ini.
Constants can also be parsed in the ini file, so if constants are defined as ini values before running parse_ini_file(), they will be integrated into the results. Only the values of ini will be evaluated.
Key names and section names composed of numbers are treated as integers by PHP, so numbers starting with 0 are treated as octal and those starting with 0x as hexadecimal.
Tips and Comments
Note:This function can be used to read the configuration file of your own application. This function is unrelated to the php.ini file, which has been processed when the script is running.
Note:If the value in the ini file contains any non-alphanumeric characters, it needs to be enclosed in double quotes (").
Note:Some reserved words cannot be used as key names in ini files, including: null, yes, no, true and false. Values of null, no and false are equivalent to "", and values of yes and true are equivalent to "1". Characters {}|"~![()" cannot be used anywhere in the key name, and these characters have special meanings in option values.
Note:Starting from PHP 5.0 version, this function also handles new lines within option values.
Example
Example 1
Content of "test.ini":
[names] me = Robert you = Peter [urls] first = "http://www.example.com" second = "http://www.codew3c.com"
PHP Code:
<?php print_r(parse_ini_file("test.ini")); ?>
Output:
Array ( [me] => Robert [you] => Peter [first] => http://www.example.com [second] => http://www.codew3c.com )
Example 2
Content of "test.ini":
[names] me = Robert you = Peter [urls] first = "http://www.example.com" second = "http://www.codew3c.com"
PHP Code (process_sections Set to true):
<?php print_r(parse_ini_file("test.ini",true)); ?>
Output:
Array ( [names] => Array ( [me] => Robert [you] => Peter ) [urls] => Array ( [first] => http://www.example.com [second] => http://www.codew3c.com ) )