【发布时间】:2011-10-04 19:34:48
【问题描述】:
下面的函数获取“test-backup.ini”文件,对其进行解析并通过 update_option() 方法将值输入数据库。
但是,当 ini 文件的值包含特殊字符,如感叹号 (!) 和等号 (=)(以及我猜的其他字符)时,它会在 parse_ini_file($file) 处引发 PHP 语法错误:
语法错误、意外的“!”等...
例如,将此内容作为 test-backup.ini 文件...
[settings]
line1 = asc
line2 = /*.blog ul li {margin-bottom:0 !important;}*/
line3 = true
line4 = <meta name="google-site-verification" content="" />
我在第 2 行遇到“!”的语法错误并在第 4 行为 "="
在传递给 parse_ini_file() 之前我应该如何过滤 $file 以处理这些字符,以便在传递给 update_option() 调用时保留它们?
到目前为止,我发现的只是:
字符 {}|&~![()" 不得在 key 中的任何位置使用,并且在 value 中具有特殊含义。 em>
$file = WP_PLUGIN_DIR.'/test/test-backup.ini';
if (file_exists($file) && is_readable($file))
{
$ini_array = parse_ini_file($file); //errors when value contains =, !, etc
foreach ($ini_array as $key=>$value) {
update_option($key, $value);
}
echo 'The settings have been saved';
}
else
{
echo 'alternate response here';
}
?>
【问题讨论】:
-
基本上,这是一个错误。您应该能够转义 = 符号。双引号有时是一种解决方法,只要您的配置管理系统工具支持它咳嗽 Augeas。
标签: php syntax-error ini