|
parse_ini_fileParse a configuration file Description
arrayfalse parse_ini_file(string
$filename , bool $process_sections = false , int $scanner_mode = INI_SCANNER_NORMAL )
parse_ini_file loads in the
ini file specified in The structure of the ini file is the same as the php.ini's. Parameters
Return Values
The settings are returned as an associative array on success,
and Examples
Example #1 Contents of sample.ini ; 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" Example #2 parse_ini_file example
Constants (but not "magic constants" like
The above example will output something similar to: Array ( [one] => 1 [five] => 5 [animal] => Dodo bird [path] => /usr/local/bin [URL] => http://www.example.com/~username [phpversion] => Array ( [0] => 5.0 [1] => 5.1 [2] => 5.2 [3] => 5.3 ) [urls] => Array ( [svn] => http://svn.php.net [git] => http://git.php.net ) ) Array ( [first_section] => Array ( [one] => 1 [five] => 5 [animal] => Dodo bird ) [second_section] => Array ( [path] => /usr/local/bin [URL] => http://www.example.com/~username ) [third_section] => Array ( [phpversion] => Array ( [0] => 5.0 [1] => 5.1 [2] => 5.2 [3] => 5.3 ) [urls] => Array ( [svn] => http://svn.php.net [git] => http://git.php.net ) ) )
Example #3 parse_ini_file parsing a php.ini file
The above example will output something similar to: (parsed) magic_quotes_gpc = Yes (loaded) magic_quotes_gpc = Yes
Example #4 Value Interpolation
In addition to evaluating constants, certain characters have special meaning in an ini value.
Additionally, environment variables and previously defined configuration options (see get_cfg_var) may be read using
; | is used for bitwise OR three = 2|3 ; & is used for bitwise AND four = 6&5 ; ^ is used for bitwise XOR five = 3^6 ; ~ is used for bitwise negate negative_two = ~1 ; () is used for grouping seven = (8|7)&(6|5) ; Interpolate the PATH environment variable path = ${PATH} ; Interpolate the configuration option 'memory_limit' configured_memory_limit = ${memory_limit}
Example #5 Escaping Characters
Some characters have special meaning in double-quoted strings and must be escaped by the backslash prefix.
First of all, these are the double quote quoted = "She said \"Exactly my point\"." ; Results in a string with quote marks in it. hint = "Use \\\" to escape double quote" ; Results in: Use \" to escape double quote There is an exception made for Windows-like paths: it's possible to not escape trailing backslash if the quoted string is directly followed by a linebreak: save_path = "C:\Temp\" If one does need to escape double quote followed by linebreak in a multiline value, it's possible to use value concatenation in the following way (there is one double-quoted string directly followed by another one): long_text = "Lorem \"ipsum\""" dolor" ; Results in: Lorem "ipsum"\n dolor
Another character with special meaning is code = "\${test}"
Escaping characters is not supported in the
Note that the ini parser doesn't support standard escape sequences ( Notes
See Also
|