Okay, here's the current version of the color escape parser.
function get_color_index($chr)
{
$chr = substr($chr, 0, 1);
return ord($chr) & 7;
}
function parse_color_escapes($str)
{
$num = preg_match_all("/\^[^\n]/", $str, $null);
$replace = array(
0 => "<span style=\"color: #222;\">",
1 => "<span style=\"color: #F00;\">",
2 => "<span style=\"color: #0F0;\">",
3 => "<span style=\"color: #FF0;\">",
4 => "<span style=\"color: #00F;\">",
5 => "<span style=\"color: #0FF;\">",
6 => "<span style=\"color: #F0F;\">",
7 => "<span style=\"color: #FFF;\">"
);
$str = str_replace(array("<", ">"), array("<", ">"), $str);
$str = str_replace(array("^<", "^>"), array("^<", "^>"), $str);
for($i = 0; $i < strlen($str); $i++) {
if($str[$i] == "^" && isset($str[$i+1]) && preg_match("/[^\n]/", $str[$i+1])) {
$c = get_color_index($str[$i+1]);
$str = substr_replace($str, $replace[$c], $i, 2);
}
}
for($i = 0; $i < $num; $i++) {
$str .= "</span>";
}
return trim($str);
}