Reputation: 497
I'm getting stumped by what should be a really simple str_replace function. In my database, I have a field for a minecraft server motd(message of the day). When I collect data from servers I store the data including the motd in my database.
Servers are able to color motds by adding a color code, which I have stored in the database as § then a number specifying the color. I'm trying to use str_replace to turn the §number into an html element with the actual color. I came up with the following code:
if ($server_data['show_motd'] == 1) {
$motd = $server_data['motd'];
$motd = str_replace("§0","</span><span style=\"color:#000\">", $motd);
$motd = str_replace("§1","</span><span style=\"color:#00A\">", $motd);
$motd = str_replace("§2","</span><span style=\"color:#0A0\">", $motd);
$motd = str_replace("§3","</span><span style=\"color:#0AA\">", $motd);
$motd = str_replace("§4","</span><span style=\"color:#A00\">", $motd);
$motd = str_replace("§5","</span><span style=\"color:#A0A\">", $motd);
$motd = str_replace("§6","</span><span style=\"color:#FA0\">", $motd);
$motd = str_replace("§7","</span><span style=\"color:#AAA\">", $motd);
$motd = str_replace("§8","</span><span style=\"color:#555\">", $motd);
$motd = str_replace("§9","</span><span style=\"color:#55F\">", $motd);
$motd = str_replace("§a","</span><span style=\"color:#5F5\">", $motd);
$motd = str_replace("§b","</span><span style=\"color:#5FF\">", $motd);
$motd = str_replace("§c","</span><span style=\"color:#F55\">", $motd);
$motd = str_replace("§d","</span><span style=\"color:#F5F\">", $motd);
$motd = str_replace("§e","</span><span style=\"color:#FF5\">", $motd);
$motd = str_replace("§f","</span><span style=\"color:#FFF\">", $motd);
echo "
<tr>
<td class=\"serverdisplayspan\">MOTD:</td>
<td class=\"serverdisplaylidet\"><span>$motd</span</td>
</tr>
";
}
However, none of the str_replace functions are doing anything. The weird thing is that when I replace
$motd = $server_data['motd'];
with
$motd = "§cWelcome to §bSkyBlock§c!"
which is what is stored in the database, it works perfectly. Without the str_replaces, the two variables look exactly the same. I've even checked the type of each variable using gettype() and both of them are strings. I've also tried utf_decode() on the database result, but nothing seems to work.
Is there some difference between the database result and the string i just typed in? I can't find out what it is but there must obviously be one.
Thanks.
Upvotes: 1
Views: 528
Reputation: 678
I had a similar issue and fixed it by using
mysqli_set_charset($con,'utf8');
right after
mysqli_connect
Upvotes: 0
Reputation: 164764
How about something like this (swaps out your inline styles for CSS classes)
$motd = preg_replace('/&(amp;)?(sect[0-9a-f])/',
'</span><span class="$2">', $motd);
Quick demo here with mixed &
and &
- http://codepad.viper-7.com/12phow
With this, you'll also want to add something like this to your stylesheet
.sect0 { color: #000; }
.sect1 { color: #00A; }
.sect2 { color: #0A0; }
/* etc */
Upvotes: 1