简体   繁体   中英

Why is str_replace not replacing this string?

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

I believe the problem might be that you're trying to replace this:

body {
    background-color: #CCCCFF;
}

by trying to find this in your CSS file:

body { background-color: #CCCCFF; }

The simplest solution on my opinion would be to just correctly format that line in CSS file

Probably the problem is a "\\n"

this line :

$css = file_get_contents($url.'/default.css');

should be

$css = file_get_contents($url.'/default.css');
$css = str_replace("\n","", $css);

Or you can use preg_replace .

file_get_contents() preserves the line breaks, but the browser does not, you are not getting anything replaced because your css file does not contain

body { background-color: #CCCCFF; }

You have been thrown off a little by your testing because when you echo out the contents of $css the line breaks appear to have been removed. This is an artifact of the browser ignoring whitespace - but you can be sure that the raw whitespace is still in the source.

You might consider preg_replace and use a regex to search for the optional line breaks. You could also do a 3-stage replacement by replacing the new lines with some other marker, doing your initial substitution, then switching the newlines back in.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM