我应该注意 parse_ini_file() 的特殊行为吗?

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

我有一个相当大的 INI 文件 (大约 100kb 超过 2000 行) 我正在尝试用 PHP 解析:$ini_array = parse_ini_file("config.ini");

但函数抛出警告:PHP Warning: syntax error, unexpected '(' in config.ini on line 1334 in ...

查看第 1334 行不会显示任何语法错误。然后我读了这个 answer :

Short answer:

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

我确信文件中没有错误,因为它在生产中被其他用 Pascal 编写的工具使用。

所以,

您可以尝试 "manual" 方法,二分法搜索。

  1. 删除第 1333 行之后的行。
  2. 然后删除(大)块之前的行,直到没有错误。
  3. 按小块添加行,直到再次出现错误。

你会发现错误的。

我在 PHP 手册中找到:

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

PHP 5.3.0我们可以设置第三个参数scanner_mode。所以 使用 INI_SCANNER_RAW 作为第三个参数调用 parse_ini_file() 成功了——该函数不计算值。

默认情况下,该函数使用 INI_SCANNER_NORMAL 模式并计算值。