Search code examples
phparraysini

Is there a special behavior of parse_ini_file() I should be aware of?


I have a quite big INI file (around 100kb and over 2000 lines) which I am trying to parse with PHP: $ini_array = parse_ini_file("config.ini");

But the function throw a warning: PHP Warning: syntax error, unexpected '(' in config.ini on line 1334 in ...

Looking at line 1334 won't show any syntax error. Then I read this answer :

Short answer:

Don't trust the line numbers, look for a syntax error in the lines before the line mentioned in the error.

I am confident that there is no error in the file because its used in production by an other tool wrote in Pascal.

So,

  • How I could check the file manually for errors ?

  • And if no syntax error exist in the file, is there a behavior of parse_ini_file() I should be aware of ?


Solution

  • I found in the PHP manual :

    Characters ?{}|&~![()^" must not be used anywhere in the key and have a special meaning in the value.

    From PHP 5.3.0 we can set a third parameter scanner_mode. So calling parse_ini_file() with INI_SCANNER_RAW as third parameter did the trick - the function do not evaluate values.

    By default the function use the INI_SCANNER_NORMAL mode and evaluate values.

    Reference: https://www.php.net/manual/function.parse-ini-string.php