PHP Function Reference

PHP parse_ini_string() Function



The PHP parse_ini_string() function parses a configuration (ini) string and returns the settings as an associative array. The structure of the ini string is the same as the php.ini's.

Note: There are reserved words which must not be used as keys for ini files. These include: null, yes, no, true, false, on, off, none. Values null, off, no and false result in "", and values on, yes and true result in "1", unless INI_SCANNER_TYPED mode is used. Characters ?{}|&~![()^" must not be used anywhere in the key and have a special meaning in the value.

Syntax

parse_ini_string(ini_string, process_sections, scanner_mode)

Parameters

ini_string Required. Specify the contents of the ini file being parsed.
process_sections Optional. If set to true, it returns a multidimensional array, with the section names and settings included. Default is false
scanner_mode Optional. It can be one of the following values:
  • INI_SCANNER_NORMAL (default)
  • INI_SCANNER_RAW (means option values will not be parsed)
  • INI_SCANNER_TYPED (means that boolean, null and integer types are preserved when possible. "true", "on", "yes" are converted to true. "false", "off", "no" and "none" are converted to false. "null" is converted to null in typed mode. Numeric strings are converted to integer type if possible)

Return Value

Returns settings as an associative array on success, and false on failure.

Example: parse_ini_string() example

The example below shows how to use the parse_ini_string() function to parse a configuration (ini) string.

<?php
$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"
';

//parsing the ini string and 
//displaying the result
print_r(parse_ini_string($ini));
?>

The output of the above code will be:

Array
(
    [one] => 1
    [five] => 5
    [animal] => 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
        )

)

Example: process sections

In the example below, the same ini string is parsed with sections.

<?php
$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"
';

//parsing with sections
print_r(parse_ini_string($ini, true));
?>

The output of the above code will be:

Array
(
    [first_section] => Array
        (
            [one] => 1
            [five] => 5
            [animal] => 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
                )

        )

)

❮ PHP Filesystem Reference