downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | conferences | my php.net

search for in the

pathinfo> <parse_ini_file
[edit] Last updated: Fri, 17 May 2013

view this page in

parse_ini_string

(PHP 5 >= 5.3.0)

parse_ini_stringAnalyse une chaîne de configuration

Description

array parse_ini_string ( string $ini [, bool $process_sections = false [, int $scanner_mode = INI_SCANNER_NORMAL ]] )

parse_ini_string() retourne la configuration dans une chaîne de type ini dans un tableau associatif.

La structure de la chaîne doit être la même que celle du fichier php.ini.

Liste de paramètres

ini

Le contenu de type ini à analyser.

process_sections

En activant le paramètre process_sections avec TRUE, vous obtiendrez un tableau multidimensionnel, avec les noms de sections et de directives. La valeur par défaut du paramètre process_sections est FALSE

scanner_mode

Peut prendre les valeurs des constantes INI_SCANNER_NORMAL (par défaut) ou INI_SCANNER_RAW. Si INI_SCANNER_RAW est utilisé, les valeurs des options ne seront pas analysées.

Valeurs de retour

Les directives sont retournées sous forme de tableau array en cas de succès, et FALSE en cas d'erreur.

Notes

Note: Il y a plusieurs mots réservés que ne doivent pas être utilisés comme clé dans les fichiers .ini. Cela inclut : null, yes, no, true, false, on, off, none. Les valeurs null, no et false sont retournées comme "" (chaîne vide); les valeurs yes, true, on sont retournées comme "1". Les caractères ?{}|&~![()^" ne doivent être utilisés nulle part dans les clés, et ont une signification spéciale dans les valeurs.

Voir aussi



pathinfo> <parse_ini_file
[edit] Last updated: Fri, 17 May 2013
 
add a note add a note User Contributed Notes parse_ini_string - [5 notes]
up
1
epicmaxim at gmail dot com
1 month ago
parse_ini_string_m is analog for a parse_ini_string function.

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

parse_ini_string_m:
- ignores commented lines that start with ";" or "#"
- ignores broken lines that do not have "="
- supports array values and array value keys

<?php
function parse_ini_string_m($str) {
   
    if(empty(
$str)) return false;

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

    foreach(
$lines as $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) {
           
           
$key = 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("^\[(.*?)\]^", $key, $matches);
            if(!empty(
$matches) &amp;&amp; isset($matches[0])) {

               
$arr_name = preg_replace('#\[(.*?)\]#is', '', $key);

                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 usage:

<?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_keys]
    val_arr_two[6] = "key_6"
    val_arr_two[some_key] = "some_key_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_keys] => Array
    (
        [val_arr_two] => Array
        (
            [6] => key_6
            [some_key] => some_key_value
        )

    )

)
up
1
alexandre at nospam dot imaginacom dot com
3 years ago
If your server has yet not been updated to PHP 5.2 (like mine), the following function can help you without having to use temporary paths.

I tried to make it the more similar possible to the PHP's behaviour, including dealing with errors or exceptions.

<?php
if(!function_exists('parse_ini_string')){
    function
parse_ini_string($str, $ProcessSections=false){
       
$lines  = explode("\n", $str);
       
$return = Array();
       
$inSect = false;
        foreach(
$lines as $line){
           
$line = trim($line);
            if(!
$line || $line[0] == "#" || $line[0] == ";")
                continue;
            if(
$line[0] == "[" && $endIdx = strpos($line, "]")){
               
$inSect = substr($line, 1, $endIdx-1);
                continue;
            }
            if(!
strpos($line, '=')) // (We don't use "=== false" because value 0 is not valid as well)
               
continue;
           
           
$tmp = explode("=", $line, 2);
            if(
$ProcessSections && $inSect)
               
$return[$inSect][trim($tmp[0])] = ltrim($tmp[1]);
            else
               
$return[trim($tmp[0])] = ltrim($tmp[1]);
        }
        return
$return;
    }
}
?>
up
2
Safak Ozpinar (safakozpinar at gmail dot com)
2 years ago
Function parse_ini_file doesn't parse a remote ini file if allow_url_include is off. But if allow_url_fopen is on, you can use parse_ini_string to parse a remote ini file after read its contents.

<?php
/**
 * Assume that; allow_url_include=0 and allow_url_fopen=1
 * (default values in php.ini)
 */
$iniUrl = 'http://example.com/remote/config.ini';

/**
 * Warning: parse_ini_file(): http:// wrapper is disabled in the server configuration by allow_url_include=0
 */
$config = parse_ini_file($iniUrl);

/**
 * works fine
 */
$config = parse_ini_string(file_get_contents($iniUrl));
?>
up
0
aldo at mschat dot net
3 years ago
If you want to use something like this on a PHP version below that of PHP 5.3, you can emulate it roughly by doing something like this:

<?php
if(!function_exists('parse_ini_string'))
{
  function
parse_ini_string($ini, $process_sections = false, $scanner_mode = null)
  {
   
# Generate a temporary file.
   
$tempname = tempnam('/tmp', 'ini');
   
$fp = fopen($tempname, 'w');
   
fwrite($fp, $ini);
   
$ini = parse_ini_file($tempname, !empty($process_sections));
   
fclose($fp);
    @
unlink($tempname);
    return
$ini;
  }
}
?>

May not be the most efficient way (I suppose you could do some regex stuff instead...) but it certainly works!
up
0
boukeversteegh at gmail dot com
3 years ago
<?php
# Define parse_ini_string if it doesn't exist.
# Does accept lines starting with ; as comments
# Does not accept comments after values
if( !function_exists('parse_ini_string') ){
   
    function
parse_ini_string( $string ) {
       
$array = Array();

       
$lines = explode("\n", $string );
       
        foreach(
$lines as $line ) {
           
$statement = preg_match(
"/^(?!;)(?P<key>[\w+\.\-]+?)\s*=\s*(?P<value>.+?)\s*$/", $line, $match );

            if(
$statement ) {
               
$key    = $match[ 'key' ];
               
$value    = $match[ 'value' ];
               
               
# Remove quote
               
if( preg_match( "/^\".*\"$/", $value ) || preg_match( "/^'.*'$/", $value ) ) {
                   
$value = mb_substr( $value, 1, mb_strlen( $value ) - 2 );
                }
               
               
$array[ $key ] = $value;
            }
        }
        return
$array;
    }
}
?>

 
show source | credits | stats | sitemap | contact | advertising | mirror sites