Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing INI file with PHP when semi-colon (;) is included in the value

I am storing translations in my INI file in my system and they are stored in this manner:

$ini=parse_ini_file('translations.ini',false,INI_SCANNER_RAW);

This INI_SCANNER_RAW setting tells PHP, according to documentation, that:

If INI_SCANNER_RAW is supplied, then option values will not be parsed.

Technically this means that it should not do any parsing with the values in the INI file, so I do not have to quote or escape anything in the INI file. These all work:

example1="one"
example2=one
example1='one'
example3="double quotes ("value")"
example4=double quotes ("value")
example3='double quotes ("value")'

They would output as:

one
one
one
double quotes ("value")
double quotes ("value")
double quotes ("value")

Even this works:

semi-colon1="ˇ1234567890+´õü'äö-.,<>~:_ÖÄ*PÕÜ`?=)(/&%¤#"!@£$€{[]}\½"

Which outputs predictably:

ˇ1234567890+´õü'äö-.,<>~:_ÖÄ*PÕÜ`?=)(/&%¤#"!@£$€{[]}\½

But here's a problem. The very moment I add a semi-colon (;) to my INI value, my parsing breaks, even if I try to escape it.

example1="semi-colon looks like (;) character"
example1="semi-colon looks like (\;) character"
example1="semi-colon looks like (\\;) character"
example1="semi-colon looks like (\\\;) character"

All of the output is:

"semi-colon looks like (
"semi-colon looks like (
"semi-colon looks like (
"semi-colon looks like (

(And the same is true if I use single quotes instead of double-quotes)

My best guess is that this is because semi-colon is considered a character for a comment, so it gets removed and it leaves this snippet of text. The starting quotes remain there because the ending quotes are after the semi-colon, thus it is not encapsulating.

But this makes little sense since # is also considered a comment symbol for INI files.

But this is a pretty serious problem for my system, how can I use a semi-colon in a value string in an INI file? Is this a bug in PHP or expected behavior?

This does not throw an exception or notice or error either.

Thank you!

like image 616
kingmaple Avatar asked Oct 25 '12 13:10

kingmaple


People also ask

What does the semi colon do in .ini files?

Semicolons (;) at the beginning of the line indicate a comment. Comment lines are ignored.

How do I read a .ini file?

How to Open and Edit INI Files. It's not a common practice for people to open or edit INI files, but they can be opened and changed with any text editor. Just double-clicking it will automatically open it in the Notepad application in Windows.

What is PHP INI file?

The php. ini file is the default configuration file for running applications that require PHP. It is used to control variables such as upload sizes, file timeouts, and resource limits. php. ini file is the configuration file.


1 Answers

This is a known bug:

  • parse_ini_file() with INI_SCANNER_RAW cuts a value that includes a semi-colon

It was fixed in PHP 5.4.5

Also fixed in PHP 5.3.15

like image 131
Gordon Avatar answered Nov 13 '22 01:11

Gordon