PHP parse_ini_file TC_CONSTANT 警告

发布于 2024-09-30 02:39:38 字数 820 浏览 10 评论 0原文

我遇到了一个奇怪的问题,奇怪的是谷歌上什么也没出现。我正在尝试解析一个充满 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

紫瑟鸿黎 2024-10-07 02:39:38

正如您所指出的,问题出在 204 = No Content 行。

这是因为 No 是 INI 文件中的一个特殊值(以及其他值)。 INI 解析器到达此行并读取 204 键的 No 值,然后找到生成的尾随 Content 文本错误。

PHP手册注释(​​例如在parse_ini_file页面上):

nullnofalse 结果为 ""yestrue 结果为 "1"

简单的修复方法是将所有值括在双引号中,或者至少将那些以 INI 关键字开头的值括起来,例如:

[codes]
100 = "Continue"
101 = "Switching Protocols"
200 = "OK"
201 = "Created"
202 = "Accepted"

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 the No value for the 204 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):

Values null, no and false results in "", yes and true results in "1".

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:

[codes]
100 = "Continue"
101 = "Switching Protocols"
200 = "OK"
201 = "Created"
202 = "Accepted"
锦上情书 2024-10-07 02:39:38

不幸的是, parse_ini_file() 所接受的内容有点狭窄。

如果 ini 文件中的值包含任何非字母数字字符,则需要用双引号 (") 括起来。

此外,这可能是错误消息的原因,我相当确定您不能使用纯因此,一定要引用您的值:

[codes]
100 = "Continue"
101 = "Switching Protocols"
200 = "OK"
...

如果有必要,请为您的键添加前缀:

[codes]
c_100 = "Continue"
c_101 = "Switching Protocols"
c_200 = "OK"

其中之一应该可以解决问题。

Unfortunately, parse_ini_file() is a bit narrow in what it accepts.

If a value in the ini file contains any non-alphanumeric characters it needs to be enclosed in double-quotes (").

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:

[codes]
100 = "Continue"
101 = "Switching Protocols"
200 = "OK"
...

and if necessary, prefix your keys:

[codes]
c_100 = "Continue"
c_101 = "Switching Protocols"
c_200 = "OK"

one of these should fix the issue.

童话 2024-10-07 02:39:38

在你的例子中,问题是“不”这个词。在引号中包含字符串

In your example the problem is word "No". Include string in quotes

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文