Parsing mIRC colors in php

喜欢而已 提交于 2019-12-09 21:06:39

问题


I want to convert mirc color codes to html via php. Here is the example: http://searchirc.com/search.php?F=exact&T=chan&N=6246&I=anime-pirates

Thanks


回答1:


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


来源:https://stackoverflow.com/questions/4503194/parsing-mirc-colors-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!