【问题标题】:PHP formatting a string to remove special character color codes and add appropriate colorPHP格式化字符串以删除特殊字符颜色代码并添加适当的颜色
【发布时间】:2013-01-29 07:10:31
【问题描述】:

Minecraft 使用某些特殊字符在其客户端上用颜色格式化字符串,我想从字符串中删除这些颜色代码,但也用适当的颜色格式化字符串。 颜色代码的示例是:“§1”和“§6” 您可以在此处查看完整列表:http://www.minecraftwiki.net/wiki/Formatting_codes

这是我的来自客户端的原始字符串示例:“§8here is the §6message of the §8day” 我需要删除“§6”颜色代码并用适当颜色的 span 标签包围文本。 这是我目前所拥有的,我无法弄清楚。

我希望这个结果是一个字符串:

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

我的功能:

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

谢谢!

【问题讨论】:

  • 最后你试图得到这样的结果:heremessage day ?
  • 是的,我用我的预期结果更新了帖子

标签: php regex string colors format


【解决方案1】:

这不是那么优雅的解决方案,但它有效,更好的解决方案是使用正则表达式,但这个对我来说更简单,所以享受吧。

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

【讨论】:

    【解决方案2】:

    使用正则表达式的另一种解决方案:

    $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 对于 PHP 5.3 和更新版本。如果您有旧版本,您可以使用 create_function() 或用户定义的函数,而不是 preg_replace_callback() 中的匿名函数。

    【讨论】:

    • 太棒了!这很好用!我尝试了一种正则表达式方法,但我无法正确使用正则表达式。另外,还有一个我从来不知道的 php 函数!谢谢!
    • 实际上,在我用我的实际消息尝试之后,它没有工作。当我以纯文本形式输入消息作为 preg_replace_callback 中的最后一个参数时,它起作用了。从数据库返回的文本(我在其中检索当天的消息)和正常工作的纯文本完全相同。当我用当天的实际消息尝试它时,它返回的是: ?002d*=@ ?0044eadman Dungeons ?0040=*- 实际的 motd 是:§8-=@ §6Deadman Dungeons §8@ =-
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-21
    • 1970-01-01
    • 2018-10-23
    • 2016-11-30
    • 2022-01-25
    相关资源
    最近更新 更多