I realize this has been asked before, on this very forum no less, but the proposed solution was not reliable for me.
I have been working on this for a week or more by now, and I stayed up 'till 3am yesterday working on it... But I digress, let me get to the issue at hand:
For those unaware, mirc uses ascii control codes to control character color, underline, weight, and italics. The ascii code for the color is 3, bold 2, underline 1F, italic 1D, and reverse(white text on black background), 16.
As an example of the form this data is going to come in, we have(in regex because those characters will not print):
\x034this text is red\x033this text is green\x03 \x02bold text\x02
\x034,3this text is red with a green background\x03
Et-cetera.
Below are the two functions I have attempted to modify for my own use, but have returned unreliable results. Before I get into that code, to be specific on 'unreliable', sometimes the code would parse, other times there would still be control codes left in the text, and I can't figure out why. Anyway;
function mirc2html($x) {
$c = array("FFF","000","00007F","009000","FF0000","7F0000","9F009F","FF7F00","FFFF00","00F800","00908F","00FFFF","0000FF","FF00FF","7F7F7F","CFD0CF");
$x = preg_replace("/\x02(.*?)((?=\x02)\x02|$)/", "<b>$1</b>", $x);
$x = preg_replace("/\x1F(.*?)((?=\x1F)\x1F|$)/", "<u>$1</u>", $x);
$x = preg_replace("/\x1D(.*?)((?=\x1D)\x1D|$)/", "<i>$1</i>", $x);
$x = preg_replace("/\x03(\d\d?),(\d\d?)(.*?)(?(?=\x03)|$)/e", "'</span><span style=\"color: #'.\$c[$1].'; background-color: #'.\$c[$2].';\">$3</span>'", $x);
$x = preg_replace("/\x03(\d\d?)(.*?)(?(?=\x03)|$)/e", "'</span><span style=\"color: #'.\$c[$1].';\">$2</span>'", $x);
//$x = preg_replace("/(\x0F|\x03)(.*?)/", "<span style=\"color: #000; background-color: #FFF;\">$2</span>", $x);
//$x = preg_replace("/\x16(.*?)/", "<span style=\"color: #FFF; background-color: #000;\">$1</span>", $x);
//$x = preg_replace("/\<\/span\>/","",$x,1);
//$x = preg_replace("/(\<\/span\>){2}/","</span>",$x);
return $x;
}
function color_rep($matches) {
$matches[2] = ltrim($matches[2], "0");
$bindings = array(0=>'white',1=>'black',2=>'blue',3=>'green',4=>'red',5=>'brown',6=>'purple',7=>'orange',8=>'yellow',9=>'lightgreen',10=>'#00908F',
11=>'lightblue',12=>'blue',13=>'pink',14=>'grey',15=>'lightgrey');
$preg = preg_match_all('/(\d\d?),(\d\d?)/',$matches[2], $col_arr);
//print_r($col_arr);
$fg = isset($bindings[$matches[2]]) ? $bindings[$matches[2]] : 'transparent';
if ($preg == 1) {
$fg = $bindings[$col_arr[1][0]];
$bg = $bindings[$col_arr[2][0]];
}
else {
$bg = 'transparent';
}
return '<span style="color: '.$fg.'; background: '.$bg.';">'.$matches[3].'</span>';
}
And, in case it is relevant, where the code is called:
$logln = preg_replace_callback("/(\x03)(\d\d?,\d\d?|\d\d?)(\s?.*?)(?(?=\x03)|$)/","color_rep",$logln);
I've of course also attempted to look at the methods done by various php/ajax based irc clients, and there hasn't been any success there. As to doing this mirc-side, I've looked there as well, and although the results have been more reliable than php, the data sent to the server increases exponentially to the point that the socket times out on upload, so it isn't a viable option.
As always, any help in this matter would be appreciated.