views:

14

answers:

2

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?

+1  A: 

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.

Pekka
of course, it just had to be simple. Thank you! I was chasing after TC_CONSTANT instead of playing with quotes. I didn't need to prefix the keys, but the quotes sure went a long way.
Noah Smith
+1  A: 

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"
salathe
Ah, excellent. So the INI parser is even more sloppy than I thought :)
Pekka