Jon McPherson
Jon McPherson

Reputation: 2535

PHP formatting a string to remove special character color codes and add appropriate color

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!

Upvotes: 1

Views: 684

Answers (2)

Placido
Placido

Reputation: 1385

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().

Upvotes: 0

Vova Lando
Vova Lando

Reputation: 558

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

Upvotes: 1

Related Questions