在PHP中调整正则表达式配置文件
我正在处理这个正则表达式:在PHP中调整正则表达式配置文件,php,regex,Php,Regex,我正在处理这个正则表达式: $regex = '~\\'.$var.'\s+=\s+\''.$oldval.'\';~is'; 这是完整的代码函数 function write_value_of($var,$oldval,$newval, $file) { $contents = file_get_contents($file); $regex = '~\\'.$var.'\s+=\s+\''.$oldval.'\';~is'; $contents = preg_rep
$regex = '~\\'.$var.'\s+=\s+\''.$oldval.'\';~is';
这是完整的代码函数
function write_value_of($var,$oldval,$newval, $file)
{
$contents = file_get_contents($file);
$regex = '~\\'.$var.'\s+=\s+\''.$oldval.'\';~is';
$contents = preg_replace($regex, "$var = '$newval';", $contents);
file_put_contents($file, $contents);
}
^正则表达式用于以下配置:
$database = 'stats';
$eCoin = '1';
但是我的配置是这样的:
database = stats
eCoin = 1
您的正则表达式应该是:
^([^\s]+)\s+=\s+([^\s]+)$
我修改了你的函数如下:
function write_value_of($var,$oldval,$newval, $file)
{
$contents = file_get_contents($file);
$regex = "/^{$var}\s+=\s+{$oldval}$/m";
$contents = preg_replace($regex, "$var = '$newval';", $contents);
file_put_contents($file, $contents);
}
可能比正则表达式更合适。谢谢,但我选择它是有原因的,你能帮我改变它吗?@PutraFajarHasanuddin:什么?你能重新措辞你的问题吗?为什么不能使用
parse_ini_file()
?像这样的一些:bot_virtualhostname=| c00FF3366VIVA,bot_commandtrigger=!