1

Minecraft uses certain special characters to format strings with colors on their client, and I want to remove those color codes from the string but also format the string with the appropriate colors. An example of the color codes are: '§1' and '§6' You can see the full list here: http://www.minecraftwiki.net/wiki/Formatting_codes

Here is an example of my string raw from the client: "§8here is the §6message of the §8day" I need to remove the '§6' color code and surround the text with span tags with the appropriate color. Here is what I have so far, and I cannot figure this out.

I would want this result as a string:

<span style='color:#55555;'>here is the </span><span style='color:#FFAA00;'> message of the</span><span style='color:#55555;'> day</span>

My function:

function formatMOTD($motd) {
$result = array();
$previous;

$result = split("§1", $motd);
if (!empty($result)) {
    foreach ($result as $value) {
        $previous .= "<span style='color:#0000AA;'>" . substr($value, 1) . "</span>";
    }
}
$result = split("§8", $motd);
if (!empty($result)) {
    foreach ($result as $value) {
        $previous .= "<span style='color:#55555;'>" . substr($value, 1) . "</span>";
    }
}
$result = split("§6", $motd);
if (!empty($result)) {
    foreach ($result as $value) {
        $previous .= "<span style='color:#FFAA00;'>" . substr($value, 1) . "</span>";
    }
}

$motd = $previous;
return $motd;
}

thanks!

2
  • At the end you trying to get like this: <span style='color:#55555;'>here</span> is the <span style='color:#FFAA00;'>message</span> of the <span style='color:#55555;'>day</span> ? Commented Feb 13, 2013 at 17:03
  • Yes I updated the post with my intended result Commented Feb 13, 2013 at 17:06

2 Answers 2

1

This is not so elegant solution, but it works, better solution would be using regex, but this one is simpler for me, so enjoy.

    function spanParser($str, $htmlColor)
    {
        $str = "<span style='color:#" . $htmlColor .";'>" . $str . "</span>";
        return $str;
    }

    $exampleString = "§8here is the §6message of the §8day";
    $arrayOfChunks = explode('§', $exampleString);
    $formatedString = "";
    foreach($arrayOfChunks as $chunk)
    {
        switch($chunk[0])
        {
        case '6':
            $chunk = substr($chunk, 1);
            $formatedString = $formatedString . spanParser($chunk, "FFAA00");
            break;
        case '8':
            $chunk = substr($chunk, 1);
            $formatedString = $formatedString . spanParser($chunk, "55555");
            break;
        default:
            break;
        }
    }

    echo $formatedString;
?>
Sign up to request clarification or add additional context in comments.

Comments

0

Another solution with regex:

$txt = "test §8here is the §6message of the §8day";
echo preg_replace_callback('/§(\d+)([^§]*)/s', 
    function($match)
    {
        $color = '';
        switch($match[1]) {
            case '1': 
                $color = '0000AA';
                break;
            case '6': 
                $color = 'FFAA00';
                break;
            case '8': 
                $color = '555555';
                break;
            default:
                break;
        }
        return "<span style='color:#" . $color .";'>" . $match[2] . "</span>";
    },
    $txt);

UPD For PHP 5.3 and newer. If you have an older version you can use create_function() or user defined function instead of anonymous one inside the preg_replace_callback().

2 Comments

Fantastic! This worked great! I tried a regex approach but I couldn't get the regex right. also, there's a php function I never knew! thanks!
actually, after I tried it with my actual message, it didn't work. When I entered the message in plain text as the last parameter in preg_replace_callback, it worked however. The returned text from the database(where I retrieve the message of the day) and the plain text that did work were the exact same. When I tried it with the actual message of the day, this is what it returned: ?002d*=@ ?0044eadman Dungeons ?0040=*- the actuall motd is: §8-=@ §6Deadman Dungeons §8@=-

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.