php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #53916 parse_ini_file unable to handle strings with certain characters
Submitted: 2011-02-03 08:56 UTC Modified: 2012-06-07 13:09 UTC
From: thasypher at gmail dot com Assigned: pierrick (profile)
Status: Not a bug Package: Filesystem function related
PHP Version: Irrelevant OS: Debian 5
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: thasypher at gmail dot com
New email:
PHP Version: OS:

 

 [2011-02-03 08:56 UTC] thasypher at gmail dot com
Description:
------------
PHP seems to be unable to handle strings with certain signs (#$%) in an INI file parsed with parse_ini_file.

It throws a warning while doing that.
Even though normally you would quote strings to overcome this issue, the ConfigParser implementation of Python (for instance) does not do this. 

Test script:
---------------
print_r(parse_ini_file('file.ini', true, ));

Expected result:
----------------
An ini file with certain characters will throw a warning and may not be parsed completely.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-02-03 09:00 UTC] thasypher at gmail dot com
-Package: Unknown/Other Function +Package: Filesystem function related
 [2011-02-03 09:00 UTC] thasypher at gmail dot com
Obviously the test script should not contain a comma next to the true value.
 [2011-02-04 10:46 UTC] aharvey@php.net
-Status: Open +Status: Feedback
 [2011-02-04 10:46 UTC] aharvey@php.net
What version of PHP are you running? This seems to behave as expected
in PHP 5.3.5.

Could you also provide a (short) sample file.ini, please?
 [2011-02-04 13:50 UTC] thasypher at gmail dot com
A sample INI could be:

----
[data]
test_case = 1
some_server = localhost
more_stuff = 8.8.8.8

[test]
somevalue = 1
password_server.domain.tld = h1b{k&Tx$jt2

----

In this case, it throws an "Error parsing <filename> on line 8.

Also with:
---
[test]
some_thing = R3iDn3!rj3j
---

it will fail.

So ISTM that special characters are causing issues in the parse_ini_file.
 [2012-06-07 08:22 UTC] zefredz@php.net
I don't know if it will help to find the cause of this bug, but I found that 
enclosing the string with quotes solves the problem :

ini file:
---------
[test]
some_thing = "R3iDn3!rj3j"

test file:
----------
<?php
print_r(parse_ini_file('bug.ini', true ));

result (as expected):
----------------------
Array ( [test] => Array ( [some_thing] => R3iDn3!rj3j ) )
 [2012-06-07 12:36 UTC] thasypher at gmail dot com
-Status: Feedback +Status: Open
 [2012-06-07 12:36 UTC] thasypher at gmail dot com
I know, but these ini's are generated by some other system that does not properly quote the values. 

As far as I know there is no requirement for ini's to have values quoted either, so it would be nice if parse_ini_file would properly handle that.
 [2012-06-07 13:09 UTC] pierrick@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: pierrick
 [2012-06-07 13:09 UTC] pierrick@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

By default the parse_ini_file function will use INI_SCANNER_NORMAL as the 3rd 
parameter. This mode will parse the option (like it is doing for php.ini) and will 
"compute" the values. That's why if you have something like this :

[test_normal]
test_or_op = 1|2
test_bool = On

The test_or_op will be 3 and test_bool will 1. You have a problem with your code 
because in this mode & ! are operators.

If you want to avoid those values to be parsed, you can use the other mode 
INI_SCANNER_RAW. This mode will parse the same ini as you could expect :

test_or_op=1|2 and test_bool=On
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 18:01:28 2024 UTC