我有以下 PHP 代码,它应该将 CSS 文件中的数据加载到变量中,搜索旧的正文背景颜色,用提交的表单中的颜色替换它,重新保存 CSS 文件,最后更新数据库中的颜色。问题是, str_replace 似乎没有替换任何东西。
这是我的 PHP 代码(存储在“processors/save_program_settings.php”中):
<?php
require("../security.php");
$institution_name = mysql_real_escape_string($_POST['institution_name']);
$staff_role_title = mysql_real_escape_string($_POST['staff_role_title']);
$program_location = mysql_real_escape_string($_POST['program_location']);
$background_colour = mysql_real_escape_string($_POST['background_colour']);
$bar_border_colour = mysql_real_escape_string($_POST['bar_border_colour']);
$title_colour = mysql_real_escape_string($_POST['title_colour']);
$url = $global_variables['program_location'];
$data_background = mysql_query("SELECT * FROM sents_global_variables WHERE name='background_colour'") or die(mysql_error());
$background_output = mysql_fetch_array($data_background);
$css = file_get_contents($url.'/default.css');
$str = "body { background-color: #".$background_output['data']."; }";
$str2 = "body { background-color: #".$background_colour."; }";
$css2 = str_replace($str, $str2, $css);
unlink('../default.css');
file_put_contents('../default.css', $css2);
mysql_query("UPDATE sents_global_variables SET data='{$institution_name}' WHERE name='institution_name'") or die(mysql_error());
mysql_query("UPDATE sents_global_variables SET data='{$staff_role_title}' WHERE name='role_title'") or die(mysql_error());
mysql_query("UPDATE sents_global_variables SET data='{$program_location}' WHERE name='program_location'") or die(mysql_error());
mysql_query("UPDATE sents_global_variables SET data='{$background_colour}' WHERE name='background_colour'") or die(mysql_error());
mysql_query("UPDATE sents_global_variables SET data='{$bar_border_colour}' WHERE name='bar_border_colour'") or die(mysql_error());
mysql_query("UPDATE sents_global_variables SET data='{$title_colour}' WHERE name='title_colour'") or die(mysql_error());
header('Location: '.$url.'/pages/start.php?message=program_settings_saved');
?>
这是我的 CSS(存储在“default.css”中):
@charset "utf-8";
/* CSS Document */
body,td,th {
font-family: Arial, Helvetica, sans-serif;
font-size: 14px;
color: #000;
}
body {
background-color: #CCCCFF;
}
.main_table th {
background:#003399;
font-size:24px;
color:#FFFFFF;
}
.main_table {
background:#FFF;
border:#003399 solid 1px;
}
.subtitle {
font-size:20px;
}
input#login_username, input#login_password {
height:30px;
width:300px;
font-size:24px;
}
input#login_submit {
height:30px;
width:150px;
font-size:16px;
}
.timetable_cell_lesson {
width:100px;
font-size:10px;
}
.timetable_cell_tutorial_a, .timetable_cell_tutorial_b, .timetable_cell_break, .timetable_cell_lunch {
width:100px;
background:#999;
font-size:10px;
}
我在 PHP 文件中使用以下代码运行了一些检查:
echo $css . "<br><br>" . $str . "<br><br>" . $str2 . "<br><br>" . $css2; exit;
它输出(如您所见,它没有改变 CSS 中的任何内容):
@charset "utf-8"; /* CSS Document */ body,td,th { font-family: Arial, Helvetica, sans-serif; font-size: 14px; color: #000; } body { background-color: #CCCCFF; } .main_table th { background:#003399; font-size:24px; color:#FFFFFF; } .main_table { background:#FFF; border:#003399 solid 1px; } .subtitle { font-size:20px; } input#login_username, input#login_password { height:30px; width:300px; font-size:24px; } input#login_submit { height:30px; width:150px; font-size:16px; } .timetable_cell_lesson { width:100px; font-size:10px; } .timetable_cell_tutorial_a, .timetable_cell_tutorial_b, .timetable_cell_break, .timetable_cell_lunch { width:100px; background:#999; font-size:10px; }
body { background-color: #CCCCFF; }
body { background-color: #FF5719; }
@charset "utf-8"; /* CSS Document */ body,td,th { font-family: Arial, Helvetica, sans-serif; font-size: 14px; color: #000; } body { background-color: #CCCCFF; } .main_table th { background:#003399; font-size:24px; color:#FFFFFF; } .main_table { background:#FFF; border:#003399 solid 1px; } .subtitle { font-size:20px; } input#login_username, input#login_password { height:30px; width:300px; font-size:24px; } input#login_submit { height:30px; width:150px; font-size:16px; } .timetable_cell_lesson { width:100px; font-size:10px; } .timetable_cell_tutorial_a, .timetable_cell_tutorial_b, .timetable_cell_break, .timetable_cell_lunch { width:100px; background:#999; font-size:10px; }