Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
对无效的ini文件使用PHP函数parse_ini_file()_Php_Parsing_Ini - Fatal编程技术网

对无效的ini文件使用PHP函数parse_ini_file()

对无效的ini文件使用PHP函数parse_ini_file(),php,parsing,ini,Php,Parsing,Ini,我有一个来自外部程序的ini文件,我无法控制它。ini文件的值周围没有双引号,例如: [Setup] C SetupCode=Code of the currently installed setup (JOW C machine configuration). Use a maximum of 8 characters. 使用parse_ini_file()会给我:语法错误,意外“ 我想我应该在php中读取原始文件,并在如下值周围添加双引号: [Setup] C SetupCode="Cod

我有一个来自外部程序的ini文件,我无法控制它。ini文件的值周围没有双引号,例如:

[Setup]
C SetupCode=Code of the currently installed setup (JOW
C machine configuration). Use a maximum of 8 characters.
使用parse_ini_file()会给我:
语法错误,意外“
我想我应该在php中读取原始文件,并在如下值周围添加双引号:

[Setup]
C SetupCode="Code of the currently installed setup (JOW
C machine configuration). Use a maximum of 8 characters."

这是最佳实践吗?如果是,我将如何做?

INI文件格式是一种非正式的标准。存在变化,请参阅。
C
似乎代表评论,但
parse_ini_file()
不理解这一点。因此,读取字符串形式的ini文件,将
C
替换为
,然后使用
parse\u ini\u string()


我要删除我的答案,但是你可以使用
str_replace
这是替换文本的最佳选择,
str_replace('C',';',$ini_string)
@Naumov str_replace在这种情况下会起作用,但不是在所有情况下都会起作用。只有当一行的第一个字符后跟空格时,C才会被替换。Ty,这对我来说很有效,我认为这是因为它缺少了值周围的双引号,但正如您所说的,在与parse_ini文件发生冲突的行前面的“Comment”可能是“C”,通常不需要在值周围加双引号。请参阅@PaulH INI文件没有正式的标准格式。这是一个非正式的标准。有些方言支持引号(特别是Windows的INF文件,它使用INI格式用于非常特殊的目的),有些方言不支持引号,有些方言可能需要引号。@Rhymoid是的,但Mini似乎认为引号对于
解析INI文件()是必要的。
@PaulH我认为Mini认为引号允许值中的换行,它通常结束一个值。
<?php
// $ini_string = file_get_contents(...);

// test data
$ini_string = "
[Setup]
C SetupCode=Code of the currently installed setup (JOW
C machine configuration). Use a maximum of 8 characters.
SetupCode=my C ode
";

// replace 'C ' at start of line with '; ' multiline
$ini_string = preg_replace('/^C /m', '; ', $ini_string);

$ini_array = parse_ini_string($ini_string);


print_r($ini_array); // outputs Array ( [SetupCode] => my C ode )