0

I come to you because to transform an ini file into an array, I use the parse_ini_file function provided by PHP.

Extract from the ini file I want to retrieve

SocEnCoursBD=001
SocEnCoursBS=001
SocEnCours$$=001
SocEnCoursLOG=001
SocEnCoursLOV=010

Unfortunately, the parse_ini_file function generates on the SocEnCours$$=001 line.

Error thrown :

syntax error, unexpected '$'

It looks like it doesn't support the $ character. I know that PHP does not support this character and that's why I use the option INI_SCANNER_RAW when calling the parse_ini_file function.

Enfin voici le bout de code PHP qui traite le fichier ini :

    public static function parse(string $path) : array {
        return file_exists($path) ? parse_ini_file($path, true, INI_SCANNER_RAW) : null;
    }

To add I use PHP 7.4. If someone has a solution, I would be interested ^^

2 Answers2

0

As specified in the notes of parse_ini_file,

Characters ?{}|&~!()^" must not be used anywhere in the key

Raw mode does not lift this restriction. While $ is not included in that list, am am pretty positive this is just missing in the doc and this DOES apply to $as well.

Johannes H.
  • 5,875
  • 1
  • 20
  • 40
  • `$` seems to be used for [value interpolation](https://www.php.net/manual/en/function.parse-ini-file.php#example-2175) – DarkBee Aug 12 '21 at 14:06
  • 1
    Yes but that does only specify its usage in values. The OP is using it in a key, which is not defined in the docs. But as all other characters used in value interpolation are mentioned as being illegal in keys, I am confident that `$` should be included in the list. – Johannes H. Aug 12 '21 at 14:12
0

I think i have found a solution. I use the library PHP component-ini. And I use this function setUseNativeFunction(false) to prevent from error like this :

syntax error, unexpected '$'

And the code snippet to deal with my problem, I attach it above :

    public static function parse(string $path) : array {
        $reader = new IniReader();
        $reader->setUseNativeFunction(false);
        return file_exists($path) ? $reader->readFile($path) : null;
    }`