PHP parse_ini_file() 函数

PHP教程评论609阅读模式

定义和用法

parse_ini_file() 函数解析一个配置文件,并以数组的形式返回其中的设置。
语法

parse_ini_file(file,process_sections)

参数_描述

file 必需。规定要检查的 ini 文件。
process_sections 可选。如果设置为 true,则返回一个多维数组,包括了配置文件中每一节的名称和设置。默认是 false。文章源自爱尚资源教程网-https://www.23jcw.net/1046.html

说明

ini 文件的结构和 php.ini 的相似。
常量也可以在 ini 文件中被解析,因此如果在运行 parse_ini_file() 之前定义了常量作为 ini 的值,将会被集成到结果中去。只有 ini 的值会被求值。
由数字组成的键名和小节名会被 PHP 当作整数来处理,因此以 0 开头的数字会被当作八进制而以 0x 开头的会被当作十六进制。文章源自爱尚资源教程网-https://www.23jcw.net/1046.html

提示和注释

注释:本函数可以用来读取你自己的应用程序的配置文件。本函数与 php.ini 文件没有关系,该文件在运行脚本时就已经处理过了。
注释:如果 ini 文件中的值包含任何非字母数字的字符,需要将其括在双引号中(")。
注释:有些保留字不能作为 ini 文件中的键名,包括:null,yes,no,true 和 false。值为 null,no 和 false 等效于 "",值为 yes 和 true 等效于 "1"。字符 {}|"~![()" 也不能用在键名的任何地方,而且这些字符在选项值中有着特殊的意义。
注释:自 PHP 5.0 版本开始,该函数也处理选项值内的新行。文章源自爱尚资源教程网-https://www.23jcw.net/1046.html

例子 1

"test.ini" 的内容:文章源自爱尚资源教程网-https://www.23jcw.net/1046.html

[names]
me = Robert
you = Peter

[urls]
first = "http://www.example.com"
second = "http://www.w3school.com.cn"

PHP 代码:文章源自爱尚资源教程网-https://www.23jcw.net/1046.html

<?php
print_r(parse_ini_file("test.ini"));
?>

输出:文章源自爱尚资源教程网-https://www.23jcw.net/1046.html

Array
(
[me] => Robert
[you] => Peter
[first] => http://www.example.com
[second] => http://www.w3school.com.cn
)

例子 2

"test.ini" 的内容:文章源自爱尚资源教程网-https://www.23jcw.net/1046.html

[names]
me = Robert
you = Peter

[urls]
first = "http://www.example.com"
second = "http://www.w3school.com.cn"

PHP 代码(process_sections 设置为 true):文章源自爱尚资源教程网-https://www.23jcw.net/1046.html

<?php
print_r(parse_ini_file("test.ini",true));
?>

输出:文章源自爱尚资源教程网-https://www.23jcw.net/1046.html

Array
(
[names] => Array
  (
  [me] => Robert
  [you] => Peter
  )
[urls] => Array
  (
  [first] => http://www.example.com
  [second] => http://www.w3school.com.cn
  )
)
文章源自爱尚资源教程网-https://www.23jcw.net/1046.html文章源自爱尚资源教程网-https://www.23jcw.net/1046.html
相关文章
版权声明:文章图片资源来源于网络,如有侵权,请留言删除!!!
  • 温馨提示:如遇到资源下载不了,或者文章没有解决你的问题的,可以联系我们帮你处理!!!
  • 转载请务必保留本文链接:https://www.23jcw.net/1046.html

发表评论