PHP parse_ini_file TC_CONSTANT 警告
我遇到了一个奇怪的问题,奇怪的是谷歌上什么也没出现。我正在尝试解析一个充满 HTTP 状态代码的 ini 文件 StatusCodes.ini。我已经在三种不同的环境中进行了测试,本地(WAMP)在共享主机(Hostmonster.com)上,现在在运行 CentOS w/ CPanel/WHM 的专用机器上。前两个环境似乎工作正常,但是在专用计算机上我收到警告:
Warning: syntax error, unexpected TC_CONSTANT in StatusCodes.ini on line 8
运行时:
$ini = parse_ini_file('StatusCodes.ini',true);
$codes = $ini['codes'];
ini 文件如下所示:
[codes] 100 = Continue 101 = Switching Protocols 200 = OK 201 = Created 202 = Accepted 203 = Non-Authoritative Information 204 = No Content 205 = Reset Content 206 = Partial Content 300 = Multiple Choices 301 = Moved Permanently 302 = Found 303 = See Other 304 = Not Modified 305 = Use Proxy 307 = Temporary Redirect 400 = Bad Request ...
如果您不想计数,则 204 = 无内容,是第 8 行。我已经把线拔掉了,没有任何变化。有什么建议吗?
I've got a weird issue, weird as in nothing comes up on Google. I'm trying to parse an ini file full of HTTP status codes, StatusCodes.ini. I've tested in three different environments, locally (WAMP) on a shared host (Hostmonster.com) and now on a dedicated machine running CentOS w/ CPanel/WHM. The first two environments seem to work fine, however on the dedicated machine I'm getting a warning:
Warning: syntax error, unexpected TC_CONSTANT in StatusCodes.ini on line 8
when running:
$ini = parse_ini_file('StatusCodes.ini',true);
$codes = $ini['codes'];
the ini file looks like:
[codes] 100 = Continue 101 = Switching Protocols 200 = OK 201 = Created 202 = Accepted 203 = Non-Authoritative Information 204 = No Content 205 = Reset Content 206 = Partial Content 300 = Multiple Choices 301 = Moved Permanently 302 = Found 303 = See Other 304 = Not Modified 305 = Use Proxy 307 = Temporary Redirect 400 = Bad Request ...
In the case you don't want to count, 204 = No Content, is line 8. I've taken the line out and nothing changes. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如您所指出的,问题出在
204 = No Content
行。这是因为
No
是 INI 文件中的一个特殊值(以及其他值)。 INI 解析器到达此行并读取204
键的No
值,然后找到生成的尾随Content
文本错误。PHP手册注释(例如在
parse_ini_file
页面上):简单的修复方法是将所有值括在双引号中,或者至少将那些以 INI 关键字开头的值括起来,例如:
The problem, as you noted, is with the line
204 = No Content
.This is because of the
No
which is a special value within the INI file (along with others). The INI parser gets to this line and reads theNo
value for the204
key, then finds the trailing<space>Content
text which generates the error.The PHP manual notes (e.g. on the page for
parse_ini_file
):The simple fix would be to enclose all of the values in double-quotes, or at the very lease those which start with the INI keywords, like:
不幸的是,
parse_ini_file()
所接受的内容有点狭窄。此外,这可能是错误消息的原因,我相当确定您不能使用纯因此,一定要引用您的值:
如果有必要,请为您的键添加前缀:
其中之一应该可以解决问题。
Unfortunately,
parse_ini_file()
is a bit narrow in what it accepts.Also, and this is probably the reason for the error message, I'm fairly sure you can't use pure numbers as keys. So definitely quote your values:
and if necessary, prefix your keys:
one of these should fix the issue.
在你的例子中,问题是“不”这个词。在引号中包含字符串
In your example the problem is word "No". Include string in quotes