parse_ini_file() - 解析一个配置文件 - php 文件目录函数
parse_ini_file()
(PHP 4, PHP 5, PHP 7)
解析一个配置文件
说明
parse_ini_file(string $filename[,bool $process_sections= false[,int $scanner_mode= INI_SCANNER_NORMAL]]): arrayparse_ini_file()载入一个由$filename指定的 ini 文件,并将其中的设置作为一个联合数组返回。
ini 文件的结构和php.ini的相似。
参数
$filename要解析的 ini 文件的文件名。
$process_sections如果将最后的$process_sections参数设为TRUE
,将得到一个多维数组,包括了配置文件中每一节的名称和设置。$process_sections的默认值是FALSE
。
Can either beINI_SCANNER_NORMAL
(default)orINI_SCANNER_RAW
.IfINI_SCANNER_RAW
is supplied, then option values will not be parsed.
返回值
成功时以关联数组array返回设置,失败时返回FALSE
。
更新日志
版本 | 说明 |
---|---|
5.3.0 | Added optional$scanner_modeparameter. Single quotes may now be used around variable assignments. Hash marks (#) may no longer be used as comments and will throw a deprecation warning if used. |
5.2.7 | On syntax error this function will returnFALSE rather than an empty array. |
5.2.4 | 由数字组成的键名和小节名会被 PHP 当作整数来处理,因此以 0 开头的数字会被当作八进制而以 0x 开头的会被当作十六进制。 |
5.0.0 | 该函数也开始处理选项值内的新行。 |
4.2.1 | 本函数也开始受到安全模式和open_basedir的影响。 |
范例
Example #1sample.ini的内容
; 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"
Example #2parse_ini_file()例子
常量也可以在 ini 文件中被解析,因此如果在运行parse_ini_file()之前定义了常量作为 ini 的值,将会被集成到结果中去。只有 ini 的值会被求值。例如:
以上例程的输出类似于:
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 ) ) )
Example #3parse_ini_file()parsing a php.ini file
以上例程的输出类似于:
(parsed) magic_quotes_gpc = Yes (loaded) magic_quotes_gpc = Yes
注释
Note:本函数和php.ini文件没有关系,该文件在运行脚本时就已经处理过了。本函数可以用来读取你自己的应用程序的配置文件。Note:
如果 ini 文件中的值包含任何非字母数字的字符,需要将其括在双引号中(")。
Note:有些保留字不能作为 ini 文件中的键名,包括:null,yes,no,true 和 false。值为 null,no 和 false 等效于"",值为 yes 和 true 等效于"1"。字符{}|&~![()"也不能用在键名的任何地方,而且这些字符在选项值中有着特殊的意义。
参见
parse_ini_string()
解析配置字符串
I use the following syntax to secure my config.ini.php file: ; Works like a charm and is both: A valid PHP File and a valid ini-File ;)
Undocumented feature! Using ${...} as a value will look to 1) an INI setting, or 2) an environment variable For example, Array ( [php_ext_dir] => ./ext/ [operating_system] => Windows_NT ) Present in PHP 5.3.2, likely in 5.x, maybe even earlier too.
note configuration files should be stored outside you www-root/htdocs folder
If your configuration file holds any sensitive information (such as database login details), remember NOT to place it within your document root folder! A common mistake is to replace config.inc.php files, which are formatted in PHP: With config.ini files which are written in plain text: [database] host = localhost The file config.ini can be read by anyone who knows where it's located, if it's under your document root folder. Remember to place it above!
Here is a quick parse_ini_file wrapper to add extend support to save typing and redundancy. Treats this ini: As if it were like this:
Besides the features mentioned above (eg. core constants, booleans), you can also access user-defined constants in ini files! This is handy if you want to create a bit-field, for example: [pizzas] ; Define pizzas hawaii = PIZZA_HAM | PIZZA_PINEAPPLE stinky = PIZZA_ONION | PIZZA_GARLIC
Somewhere between versions 5.2.5 and 5.3.24, the parsing of unquoted multiword values (e.g. values with embedded spaces) changed. In 5.3.24, a multiword value where one of the words is a reserved word (null, yes, no, true, false, on, off, none) will cause the function to return an error. Adding double quotation marks around the value string will solve the problem.
And for the extra-paranoid like myself, add a rule into your httpd.conf file so that *.ini (or *.inc) in my case can't be sent to a browser: Order deny,allow Deny from all
To those who were like me looking if this could be used to create an array out of commandline output I offer you the function below (I used it to parse mplayer output). If you want it behave exactly the same as parse_ini_file you'll obviously have to add some code to feed the different sections to this one. Hope it's of help to someone!
a ini lexer with regexp:
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!