开发者

PHP parse_ini_file TC_CONSTANT warning

开发者 https://www.devze.com 2023-01-22 23:06 出处:网络
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 (WAM

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?


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"


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消