kingmaple
kingmaple

Reputation: 4320

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!

Upvotes: 10

Views: 2870

Answers (1)

Related Questions