gpt4 book ai didi

php - 当文件的值包含感叹号和等号时,使用 parse_ini_file() 出现语法错误

转载 作者:行者123 更新时间:2023-12-02 23:45:09 32 4
gpt4 key购买 nike

下面的函数获取“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 行的“=”

在将 $file 传递给 parse_ini_file() 之前,我应该如何过滤 $file 以处理这些字符,以便在传递给 update_option() 调用时保留它们?

到目前为止我发现的是:

字符 {}|&~![()"不得在中的任何位置使用,并且在中具有特殊含义。

$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';
}

?>

最佳答案

您应该这样将您的值放在双引号之间:

line1 = asc
line2 = "/*.blog ul li {margin-bottom:0 !important;}*/"
line3 = true
line4 = "<meta name=\"google-site-verification\" content=\"\" />"

希望这有帮助

关于php - 当文件的值包含感叹号和等号时,使用 parse_ini_file() 出现语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6684800/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com