开发者

Parsing mIRC colors in php

开发者 https://www.devze.com 2023-01-31 11:28 出处:网络
I want to convert mirc color codes to html via php. Here i开发者_高级运维s the example: http://searchirc.com/search.php?F=exact&T=chan&N=6246&I=anime-pirates

I want to convert mirc color codes to html via php. Here i开发者_高级运维s the example: http://searchirc.com/search.php?F=exact&T=chan&N=6246&I=anime-pirates

Thanks


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号