我试图使用下面的函数替换不同主题文件中的某些代码,但是,我在函数的第二部分停留了,我想替换一些包含单引号的PHP代码。
运行该函数时,唯一的更改是第一部分。
function update_GTour_theme_files()
{
$new_update = file_get_contents("/home/tourieuw/public_html/wp-content/themes/grandtour/header.php");
$new_update = preg_replace('/\$page_menu_transparent = 1/', '\$page_menu_transparent = 0', $new_update);
$new_update = preg_replace('/\$grandtour_page_menu_transparent = 1/', '\$grandtour_page_menu_transparent = 0', $new_update);
if (file_put_contents("/home/tourieuw/public_html/wp-content/themes/grandtour/header.php", $new_update)) {
$errpass = TRUE;
} else {
$errmsg = "Header.php was not updated";
$errpass = FALSE;
}
$new_update_2 = file_get_contents("/home/tourieuw/public_html/wp-content/themes/grandtour/templates/template-tour-header.php");
$new_update_2 = preg_replace('/(esc_html(grandtour_format_tour_price($tour_price)))/', '\'From \'.esc_html(grandtour_format_tour_price($tour_price)', $new_update_2);
if (file_put_contents("/home/tourieuw/public_html/wp-content/themes/grandtour/templates/template-tour-header.php", $new_update_2)) {
$errpass = TRUE;
} else {
$errmsg = "template-tour-header.php was not updated";
$errpass = FALSE;
}
if ($errpass = TRUE) {
echo '</br><span style="color:green;font-weight:bold;">Changes were applied successfully.</span>';
} else {
echo '</br><span style="color:red;font-weight:bold;">' . $errmsg . '</span>';
}
}
我希望在运行此功能时,两个文件中的两个变量都将被此代码替换