【问题标题】:Parsing mIRC colors in php在 php 中解析 mIRC 颜色
【发布时间】:2011-05-29 00:58:17
【问题描述】:

我想通过 php 将mirc color codes 转换为 html。 示例如下:http://searchirc.com/search.php?F=exact&T=chan&N=6246&I=anime-pirates

谢谢

【问题讨论】:

  • 我在您的链接示例中没有看到任何我理解的内容。你能描述一下颜色代码的工作原理吗?
  • mirc.com/help/colors.html 请看一下。基本上,在“ctrl + k”组合之后,我们为每种颜色键入一个数字。如果我们在数字之间使用逗号,我们会得到背景...
  • 你要解析什么?数据将以什么形式出现?输入源/方法是什么? PHP需要如何设置?你会使用预设的 HTML 类还是只设置背景颜色?

标签: php colors mirc


【解决方案1】:

使用preg_replace_callback:

function mycallback($matches) {
    $bindings = array(
       0=>'white',
       1=>'black',
       2=>'blue',
       3=>'green',
       4=>'red',
       5=>'brown',
       6=>'purple',
    );

    $fg = isset($bindings[$matches[1]]) ? $bindings[$matches[1]] : 'transparent';
    $bg = isset($bindings[$matches[2]]) ? $bindings[$matches[2]] : 'transparent';

    return '<span style="color: '.$fg.'; background: '.$bg.';">'.$matches[3].'</span>';
}

$str = '^C3,1Hello^C foo ^C6,2World^C';
$str = preg_replace_callback('/\^C([0-9]{1,2}),?([0-9]{1,2})(.*?)\^C/', 'mycallback', $str);

echo $str;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多