php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #49461 parse_ini_file: semicolon in section header
Submitted: 2009-09-03 20:17 UTC Modified: 2010-12-20 13:27 UTC
Votes:22
Avg. Score:4.5 ± 0.9
Reproduced:20 of 20 (100.0%)
Same Version:17 (85.0%)
Same OS:10 (50.0%)
From: sebastian dot schleussner at angstrom dot uu dot se Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.3.0 OS: Linux
Private report: No CVE-ID: None
 [2009-09-03 20:17 UTC] sebastian dot schleussner at angstrom dot uu dot se
Description:
------------
This is a follow-up to Bug #49443.
The character breaking parse_ini_file of PHP 5.3.0 with browscap.ini is actually the comment character ";" inside section headers - not one of the special characters.


Reproduce code:
---------------
;sample1.ini
; demonstration of what works in 5.2 and 5.3
[a(b){c}&~![^]
x=y

;sample2.ini
; demonstration of the problem
[a;b];c
x=y

Code:
-----
<?php
print_r(parse_ini_file('sample1.ini', true));
print_r(parse_ini_file('sample2.ini', true));
?>

Expected result:
----------------
As in PHP 5.2.10 -- the header's square brackets being interpreted as quoting:
Array
(
    [a(b){c}&~![^] => Array
        (
            [x] => y
        )

)
Array
(
    [a;b] => Array
        (
            [x] => y
        )

)

Actual result:
--------------
Array
(
    [a(b){c}&~![^] => Array
        (
            [x] => y
        )

)
PHP Warning:  syntax error, unexpected $end, expecting ']' in sample2.ini on line 1

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-09-09 05:40 UTC] oc3ans at gmail dot com
According to accepted standards of ini files the semicolon is starting a 
comment that lasts till the end of the line, so IMHO this is not a bug, 
if you want to include semicolons in the keys or sections you should 
escape it with a backslash.
 [2009-09-09 06:30 UTC] sebastian dot schleussner at angstrom dot uu dot se
Okay, classification as "bug" may be debatable, and note that I have made this a "Feature/Change Request".
At the very least, it is an undocumented and irritating change of functionality.

Next, it is a change that breaks normal parsing of the widely used browscaps.ini, which PHP's own get_browser still reads flawlessly.

Most importantly, semicolons ARE allowed inside quotes, and I think it is very logical to interpret the square brackets of section titles as quoting, too (as pre-5.3 PHP did). There is no legitimate use of a semicolon *for starting a comment* before the closing square bracket, so there is no reason to interpret it as such. If one wants to add a comment on the title line, one can do so after the closing bracket.

As to accepted standards: On the one hand my experience is that there is a lot of variation in the format of INI files, so a parser should be reasonably lenient. On the other hand I have never seen backslash escaping in INI files and I'm not at all sure it is part of any "accepted standard" for them.
It only works partly anyway, and inconsistently.
Take this file:
;sample3.ini
[a\;b];c
x=y\;z
y="a;b"
z="a\;b"
and run print_r(parse_ini_file("sample3.ini", true));
You get (PHP 5.2.10 and 5.3.0):
Array
(
    [a\;b] => Array
        (
            [x] => y\
            [y] => a;b
            [z] => a\;b
        )

)
No bailout, but (a) the backslash remains inside title and quotes, (b) the escaping does not work in values. No good, IMHO.
Variable z shows that titles and quoted strings are still considered equally in terms of backslashes, and x demos that unquoted ;'s are always filtered, but while the unescaped ; in y is allowed, it's not in the title in 5.3 -- that's inconsistent and should be reverted.

I rest my case.
 [2009-09-09 06:45 UTC] sebastian dot schleussner at angstrom dot uu dot se
Just tried the second part of your suggestion, oc3ans. Here's another inconsistency.
A *key* with an escaped semicolon is *ignored* in PHP 5.2.10.
But in PHP 5.3.0 it causes the parsing to quit silently - it does not fail as with unescaped semicolons in sections, and returns the lines before the "malformed" one, but does not read any further.
 [2010-12-20 13:27 UTC] jani@php.net
-Status: Open +Status: Bogus -Package: Feature/Change Request +Package: *General Issues
 [2010-12-20 13:27 UTC] jani@php.net
See the 3rd (optional) parameter to parse_ini_file(), INI_SCANNER_RAW is used internally for the browscap stuff.
 [2010-12-21 09:27 UTC] wangyi6854 at sohu dot com
How can I get right result with get_browser()? The function can't take 'INI_SCANNER_RAW' like parameter.
 [2011-01-19 17:24 UTC] norman dot albusberger at profiflitzer dot de
Has anybody a solution for the browscap problem? There is a way if you use the 
parse_ini_file-function but the browscap.ini (like most other inis) is parsed by 
php itself on startup of the web server. Is there a way to set params how the inis 
are parsed? I dont really like to downgrade to php 5.2. because its much faster.
Or how do i have to edit the browscap.ini to work with php 5.3?
Thank you.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jul 27 07:00:02 2025 UTC