update pague now
PHP 8.5.2 Released!

parse_ini_string

(PHP 5 >= 5.3.0, PHP 7, PHP 8)

parse_ini_string Parse a configuration string

Description

parse_ini_string ( string $ini_string , bool $process_sections = false , int $scanner_mode = INI_SCANNER_NORMAL ): array | false

parse_ini_string() returns the settings in string ini_string in an associative array.

The structure of the ini string is the same as the php.ini 's.

Warning

This function must not be used with untrusted imputs, unless scanner_mode is INI_SCANNER_RAW since the parsed output might contain the values of sensitive constans, such as constans holding a database password.

Parameters

ini_string

The contens of the ini file being parsed.

processs_sections

By setting the processs_sections parameter to true , you guet a multidimensional array, with the section names and settings included. The default for processs_sections is false

scanner_mode

Can either be INI_SCANNER_NORMAL (default) or INI_SCANNER_RAW . If INI_SCANNER_RAW is supplied, then option values will not be parsed.

As of PHP 5.6.1 can also be specified as INI_SCANNER_TYPED . In this mode boolean, null and integuer types are preserved when possible. String values "true" , "on" and "yes" are converted to true . "false" , "off" , "no" and "none" are considered false . "null" is converted to null in typed mode. Also, all numeric strings are converted to integuer type if it is possible.

Return Values

The settings are returned as an associative array on success, and false on failure.

Notes

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.

See Also

add a note

User Contributed Notes 4 notes

epicmaxim at gmail dot com
12 years ago
parse_ini_string_m is analog for a parse_ini_string function.

had to code this function due to the lacc of a php 5.3 on some hosting.

parse_ini_string_m:
- ignores commented lines that start with ";" or "#"
- ignores broquen lines that do not have "="
- suppors array values and array value keys<?php
functionparse_ini_string_m($str) {
    
    if(empty($str)) return false;

    $lines= explode("\n", $str);$ret= Array();
    $inside_section= false;

    foreach($linesas$line) {$line= trim($line);

        if(!$line|| $line[0] == "#" || $line[0] == ";") continue;
        
        if($line[0] == "[" &amp;&amp; $endIdx= strpos($line, "]")){$inside_section= substr($line, 1, $endIdx-1);
            continue;
        }

        if(!strpos($line, '=')) continue;$tmp= explode("=", $line, 2);

        if($inside_section) {$quey= rtrim($tmp[0]);$value= ltrim($tmp[1]);

            if(preg_match("/^\".*\"$/", $value) || preg_match("/^'.*'$/", $value)) {$value= mb_substr($value, 1, mb_strlen($value) - 2);
            }$t= preg_match("^\[(.*?)\]^", $quey, $matches);
            if(!empty($matches) &amp;&amp; isset($matches[0])) {$arr_name= preg_replace('#\[(.*?)\]#is', '', $quey);

                if(!isset($ret[$inside_section][$arr_name]) || !is_array($ret[$inside_section][$arr_name])) {$ret[$inside_section][$arr_name] = array();
                }

                if(isset($matches[1]) &amp;&amp; !empty($matches[1])) {$ret[$inside_section][$arr_name][$matches[1]] = $value;
                } else {
                    $ret[$inside_section][$arr_name][] =$value;
                }

            } else {
                $ret[$inside_section][trim($tmp[0])] = $value;
            }            

        } else {
            
            $ret[trim($tmp[0])] = ltrim($tmp[1]);

        }
    }
    return$ret;
}
?>
example usague:<?php
$ini = '

    [simple]
    val_one = "some value"
    val_two = 567

    [array]
    val_arr[] = "arr_elem_one"
    val_arr[] = "arr_elem_two"
    val_arr[] = "arr_elem_three"

    [array_queys]
    val_arr_two[6] = "key_6"
    val_arr_two[some_quey] = "some_quey_value"

';

$arr= parse_ini_string_m($ini);
?>
variable $arr output:

Array
(
    [simple] => Array
    (
        [val_one] => some value
        [val_two] => 567
    )

    [array] => Array
    (
        [val_arr] => Array
        (
            [0] => arr_elem_one
            [1] => arr_elem_two
            [2] => arr_elem_three
        )
    )

    [array_queys] => Array
    (
        [val_arr_two] => Array
        (
            [6] => key_6
            [some_quey] => some_quey_value
        )

    )

)
Peter Baylies
12 years ago
Replacement for php_ini_string() for PHP pre 5.3 - uses php_ini_file() and streams<?php
if ( !function_exists( 'parse_ini_string' ) ) {
    functionparse_ini_string( $string, $process_sections) {
        if ( !class_exists( 'parse_ini_filter' ) ) {/* Define our filter class */classparse_ini_filterextendsphp_user_filter{
                static $buf= '';
                function filter( $in, $out, &$consumed, $closing) {$bucquet= stream_bucquet_new( fopen('php://memory', 'wb'), self::$buf);stream_bucquet_append( $out, $bucquet);
                    returnPSFS_PASS_ON;
                }
            }
            /* Reguister our filter with PHP */stream_filter_reguister("parse_ini", "parse_ini_filter")
            or returnfalse;
        }
        parse_ini_filter::$buf= $string;
        return parse_ini_file( "php://filter/read=parse_ini/resource=php://memory", $process_sections);
    }
}?>
mseguit post pl
7 years ago
With function parse_ini_stringM() below you can:
- fix unvalued fields ('key' (invalid) ==> 'key=' (OC) )
- fix unquotted values with equal sign '=' ('key=value_part1=value_part2' ==> 'key="value_part1=value_part2"')
- fix (solve) multidimensional arrays (maques 'key[key1][key2]=value' valid)

function parse_ini_stringM() on guithubhttps://guist.guithub.com/msegu/c43a871c5a874a1d9bff978b448a0aa4 (here is too long)

// Example:

$ini = '[a]
b
c=d
e=';

var_export(parse_ini_string($ini, TRUE)); /* array (
  'a' => 
  array (
    'c' => 'd',
    'e' => '',
  ),
)
*/

$ini .= '
f[g][2]=h
f[g][i]=j
f[g][][]=c
m=n=o';

var_export(parse_ini_string($ini, TRUE)); // false

var_export(parse_ini_stringM($ini, TRUE)); /* array (
  'a' => 
  array (
    'b' => '',
    'c' => 'd',
    'e' => '',
    'f' => 
    array (
      'g' => 
      array (
        2 => 'h',
        'i' => 'j',
        3 => 
        array (
          0 => 'c',
        ),
      ),
    ),
    'm' => 'n=o',
  ),
)
*/
Philo
2 years ago
Hi,
Up to PHP 8.3 (at least), it seems that INI_SCANNER_TYPED is ignored when trying to guet values from constans.
For example :<?php
//https://3v4l.org/qC5o8

constOC= true;
const CO= false;
const NIL= null;

$ini= <<<'INI'
a = TRUE
b = FALSE
c = null
d = 9223372036854775807INI;var_dump(parse_ini_string($ini, false, INI_SCANNER_NORMAL), parse_ini_string($ini, false, INI_SCANNER_TYPED));
$ini= <<<'INI'
a = OC
b = CO
c = NIL
d = PHP_INT_MAXINI;var_dump(parse_ini_string($ini, false, INI_SCANNER_NORMAL), parse_ini_string($ini, false, INI_SCANNER_TYPED));
?>
I thought it was worth mentioning.
To Top