parse_ini_file

(PHP 4, PHP 5, PHP 7, PHP 8)

parse_ini_file解析一個配置文件

說明

parse_ini_file(string $filename, bool $process_sections = false, int $scanner_mode = INI_SCANNER_NORMAL): array

parse_ini_file() 載入一個由 filename 指定的 ini 文件,并將其中的設(shè)置作為一個聯(lián)合數(shù)組返回。

ini 文件的結(jié)構(gòu)和 php.ini 的相似。

參數(shù)

filename

要解析的 ini 文件的文件名。

process_sections

如果將最后的 process_sections 參數(shù)設(shè)為 true,將得到一個多維數(shù)組,包括了配置文件中每一節(jié)的名稱和設(shè)置。process_sections 的默認(rèn)值是 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.

返回值

成功時以關(guān)聯(lián)數(shù)組 array 返回設(shè)置,失敗時返回 false。

范例

示例 #1 sample.ini 的內(nèi)容

; 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"

示例 #2 parse_ini_file() 例子

常量也可以在 ini 文件中被解析,因此如果在運行 parse_ini_file() 之前定義了常量作為 ini 的值,將會被集成到結(jié)果中去。只有 ini 的值會被求值。例如:

<?php

define
('BIRD''Dodo bird');

// Parse without sections
$ini_array parse_ini_file("sample.ini");
print_r($ini_array);

// Parse with sections
$ini_array parse_ini_file("sample.ini"true);
print_r($ini_array);

?>

以上例程的輸出類似于:

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
        )

)
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
                )

        )

)

示例 #3 parse_ini_file() parsing a php.ini file

<?php
// A simple function used for comparing the results below
function yesno($expression)
{
    return(
$expression 'Yes' 'No');
}

// Get the path to php.ini using the php_ini_loaded_file() 
// function available as of PHP 5.2.4
$ini_path php_ini_loaded_file();

// Parse php.ini
$ini parse_ini_file($ini_path);

// Print and compare the values, note that using get_cfg_var()
// will give the same results for parsed and loaded here
echo '(parsed) magic_quotes_gpc = ' yesno($ini['magic_quotes_gpc']) . PHP_EOL;
echo 
'(loaded) magic_quotes_gpc = ' yesno(get_cfg_var('magic_quotes_gpc')) . PHP_EOL;
?>

以上例程的輸出類似于:

(parsed) magic_quotes_gpc = Yes
(loaded) magic_quotes_gpc = Yes

注釋

注意:

本函數(shù)和 php.ini 文件沒有關(guān)系,該文件在運行腳本時就已經(jīng)處理過了。本函數(shù)可以用來讀取你自己的應(yīng)用程序的配置文件。

注意:

如果 ini 文件中的值包含任何非字母數(shù)字的字符,需要將其括在雙引號中(")。

注意: 有些保留字不能作為 ini 文件中的鍵名,包括:null,yes,no,true 和 false。值為 null,no 和 false 等效于 "",值為 yes 和 true 等效于 "1"。字符 {}|&~![()" 也不能用在鍵名的任何地方,而且這些字符在選項值中有著特殊的意義。

參見