【发布时间】: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;
}
谢谢!
【问题讨论】:
-
最后你试图得到这样的结果:here 是 message day 的 ?
-
是的,我用我的预期结果更新了帖子
标签: php regex string colors format