Why is str_replace not replacing this string?
Posted
by
Niall
on Stack Overflow
See other posts from Stack Overflow
or by Niall
Published on 2012-12-16T16:49:24Z
Indexed on
2012/12/16
17:03 UTC
Read the original article
Hit count: 277
I have the following PHP code which should load the data from a CSS file into a variable, search for the old body background colour, replace it with the colour from a submitted form, resave the CSS file and finally update the colour in the database. The problem is, str_replace does not appear to be replacing anything.
Here is my PHP code (stored in "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');
?>
Here is my CSS (stored in "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;
}
I've run some checks using the following code in the PHP file:
echo $css . "<br><br>" . $str . "<br><br>" . $str2 . "<br><br>" . $css2; exit;
And it outputs (as you can see it's not changing anything in the 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; }
© Stack Overflow or respective owner