PHP-解析ini文件并访问单个值 [英] PHP - Parse ini file and access single values
问题描述
在Python中,您可以解析.ini
文件并按如下方式访问单个值:
In Python you can parse an .ini
file and access the single values like this:
myini.ini
[STRINGS]
mystring = fooooo
value = foo_bar
script.py
import configparser
config = configparser.ConfigParser()
# ----------------------------------------------------------------------
config.read("myini.ini")
test = config["STRINGS"]["mystring"]
print (test) #-> OUTPUT: fooooo
如何在PHP中做同样的事情?不幸的是,我找不到任何示例.
How can I do the same in PHP? Unfortunately, I was not able to find any examples.
推荐答案
不用担心,解析.ini
文件是一种标准方法. (请参阅php文档中的 parse_ini_file ).
Not to fear, parsing an .ini
file is a standard method. (See parse_ini_file in the php docs).
使用文件作为本示例的基础:
Using your file as the base of this example:
myini.ini
[STRINGS]
mystring = fooooo
value = foo_bar
test.php
$ini_array = parse_ini_file("myini.ini");
print_r($ini_array); # prints the entire parsed .ini file
print($ini_array['mystring']); #prints "fooooo"
请注意,默认情况下,parse_ini_file
会忽略节并将所有ini设置合并到同一对象中.如果您希望像python示例中那样分段设置范围,请为process_sections
参数(第二个参数)传递true
.
Note that by default parse_ini_file
ignores sections and gloms all ini settings into the same object. If you'd like to have things scoped sectionally as in your python example, pass true
for the process_sections
parameter (second parameter).
test2.php
$ini_array = parse_ini_file("myini.ini", true /* will scope sectionally */);
print($ini_array['mystring']); #prints nothing
print($ini_array['STRINGS']['mystring']); #prints fooooo
这篇关于PHP-解析ini文件并访问单个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!