mongy910
mongy910

Reputation: 497

str_replace not working on database return

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 &sect then a number specifying the color. I'm trying to use str_replace to turn the &sectnumber 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("&sect0","</span><span style=\"color:#000\">", $motd);
        $motd = str_replace("&sect1","</span><span style=\"color:#00A\">", $motd);
        $motd = str_replace("&sect2","</span><span style=\"color:#0A0\">", $motd);
        $motd = str_replace("&sect3","</span><span style=\"color:#0AA\">", $motd);
        $motd = str_replace("&sect4","</span><span style=\"color:#A00\">", $motd);
        $motd = str_replace("&sect5","</span><span style=\"color:#A0A\">", $motd);
        $motd = str_replace("&sect6","</span><span style=\"color:#FA0\">", $motd);
        $motd = str_replace("&sect7","</span><span style=\"color:#AAA\">", $motd);
        $motd = str_replace("&sect8","</span><span style=\"color:#555\">", $motd);
        $motd = str_replace("&sect9","</span><span style=\"color:#55F\">", $motd);
        $motd = str_replace("&secta","</span><span style=\"color:#5F5\">", $motd);
        $motd = str_replace("&sectb","</span><span style=\"color:#5FF\">", $motd);
        $motd = str_replace("&sectc","</span><span style=\"color:#F55\">", $motd);
        $motd = str_replace("&sectd","</span><span style=\"color:#F5F\">", $motd);
        $motd = str_replace("&secte","</span><span style=\"color:#FF5\">", $motd);
        $motd = str_replace("&sectf","</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 = "&sectcWelcome to &sectbSkyBlock&sectc!"

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

Answers (2)

drooh
drooh

Reputation: 678

I had a similar issue and fixed it by using

mysqli_set_charset($con,'utf8'); 

right after

mysqli_connect

Upvotes: 0

Phil
Phil

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 &amp; - 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

Related Questions