2

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

Brad Christie
  • 100,477
  • 16
  • 156
  • 200
wonnie
  • 459
  • 3
  • 6
  • 19
  • I don't see anything I understand in your linked example. Can you describe how the colour codes work? – Pekka Dec 21 '10 at 19:37
  • http://www.mirc.com/help/colors.html please take a look at it. Basicly, after the "ctrl + k" combination we type a number for each color. If we use comma between to numbers, we get background... – wonnie Dec 21 '10 at 19:40
  • What do you want to parse? What form will the data be coming in? What is the input source/method? How does the PHP need set up? Will you be using preset HTML classes or just setting the background color? – ssube Dec 21 '10 at 19:46

1 Answers1

5

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;
netcoder
  • 66,435
  • 19
  • 125
  • 142