2

我想通过 php 将mirc 颜色代码转换为 html。这是示例:http://searchirc.com/search.php?F=exact&T=chan&N=6246&I=anime-pirates

谢谢

4

1 回答 1

5

使用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;
于 2010-12-21T20:11:46.730 回答