Filesystem 函数
在线手册:中文  英文

parse_ini_string

(PHP 5 >= 5.3.0)

parse_ini_stringParse a configuration string

说明

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

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

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

参数

ini

The contents of the ini file being parsed.

process_sections

By setting the process_sections parameter to TRUE, you get a multidimensional array, with the section names and settings included. The default for process_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.

返回值

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

注释

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, no and false results in "", yes and true results in "1". Characters ?{}|&~![()^" must not be used anywhere in the key and have a special meaning in the value.

参见


Filesystem 函数
在线手册:中文  英文

用户评论:

epicmaxim at gmail dot com (2013-04-04 07:51:56)

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($line1$endIdx-1);
            continue;
        }

        if(!
strpos($line'=')) continue;

        
$tmp explode("="$line2);

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

            if(
preg_match("/^\".*\"$/"$value) || preg_match("/^'.*'$/"$value)) {
                
$value mb_substr($value1mb_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
        )

    )

)

易百教程