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 = "http://www.w3cschool.cc"
me = Robert
you = Peter
[urls]
first = "http://www.example.com"
second = "http://www.w3cschool.cc"
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] => http://www.w3cschool.cc
)
(
[me] => Robert
[you] => Peter
[first] => http://www.example.com
[second] => http://www.w3cschool.cc
)
實例2
"test.ini" 的內容:
[names]
me = Robert
you = Peter
[urls]
first = "http://www.example.com"
second = "http://www.w3cschool.cc"
me = Robert
you = Peter
[urls]
first = "http://www.example.com"
second = "http://www.w3cschool.cc"
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.w3cschool.cc
)
)
(
[names] => Array
(
[me] => Robert
[you] => Peter
)
[urls] => Array
(
[first] => http://www.example.com
[second] => http://www.w3cschool.cc
)
)
完整的PHP Filesystem參考手冊