Сегодня я написал функцию-конвертер форматированного текста, скопированного из окна mIRC (или другого IRC-клиента) в HTML-представление. Назвал функцию ParseIRChat, хотя подходящие названия mIRC2HTML, irc to html и т.д. Функция принимает текст из чата и возвращает HTML-код. Написана на PHP. Открыта как обычно под лицензией BSD.
Версия 1.0.0.0 beta.
Итак, приведу ниже код функции. Скачать PHP-файл можно здесь: mircChat2html.zip, mircChat2html.tar.gz. А online demo можно посмотреть тут.
-
<?php
-
-
/**
-
* mIRC-Chat text highlighter.
-
* Get mIRC-formatted string and returnes it in HTML-format
-
* Color scheme is near mIRC default (light scheme)
-
*
-
* @param string $string
-
* @return string
-
*/
-
function ParseIRChat($string)
-
{
-
-
$colors = array(
-
0 => ‘white’,
-
1 => ‘black’,
-
2 => ‘blue’,
-
3 => ‘green’,
-
4 => ‘#FA5A5A’, //lightred
-
5 => ‘brown’,
-
6 => ‘purple’,
-
7 => ‘orange’,
-
8 => ‘yellow’,
-
9 => ‘lightgreen’,
-
10 => ‘cyan’,
-
11 => ‘lightcyan’,
-
12 => ‘lightblue’,
-
13 => ‘pink’,
-
14 => ‘grey’,
-
15 => ‘lightgrey’
-
);
-
-
$modes = array(
-
‘bold’ => false,
-
‘underlined’ => false,
-
‘reverse’ => false,
-
‘currentFG’ => »,
-
‘currentBG’ => »,
-
);
-
-
$string = nl2br($string) . ‘’;
-
-
$resultString = »;
-
$i = 0;
-
while ( $i < strlen($string) )
-
{
-
switch ( $string[$i] )
-
{
-
// Bold — Control+B
-
case ‘’:
-
if ( $modes[‘underlined’] )
-
$resultString .= ‘</u>’;
-
-
if ( $modes[‘bold’] = !$modes[‘bold’] )
-
$resultString .= ‘<strong>’;
-
else
-
$resultString .= ‘</strong>’;
-
-
if ( $modes[‘underlined’] )
-
$resultString .= ‘<u>’;
-
$i++;
-
break;
-
// Undelined — Control+U
-
case ‘’:
-
if ( $modes[‘underlined’] )
-
$resultString .= ‘</bold>’;
-
-
if ( !($modes[‘underlined’] = !$modes[‘underlined’]) )
-
$resultString .= ‘</u>’;
-
else
-
$resultString .= ‘<u>’;
-
-
if ( $modes[‘underlined’] )
-
$resultString .= ‘<bold>’;
-
$i++;
-
break;
-
// Reverse — Control+R
-
case ‘’:
-
if ( $modes[‘bold’] )
-
$resultString .= ‘</strong>’;
-
if ( $modes[‘underlined’] )
-
$resultString .= ‘</u>’;
-
-
if ( $modes[‘reverse’] = !$modes[‘reverse’] )
-
{
-
if ( $modes[‘currentBG’] || $modes[‘currentFG’] )
-
$resultString .= ‘</span>’;
-
$resultString .= ‘<span style=»color:’.$colors[0].‘;background-color:’.$colors[1].‘;»>’;
-
}
-
else
-
{
-
$resultString .= ‘</span>’;
-
if ( $modes[‘currentBG’] || $modes[‘currentFG’] )
-
$resultString .= ‘<span style=»‘.($modes[‘currentFG’] ? «color:{$modes[‘currentFG’]};»:»).($modes[‘currentBG’] ? «background-color:{$modes[‘currentBG’]};»:»).‘»>’;
-
}
-
-
if ( $modes[‘bold’] )
-
$resultString .= ‘<strong>’;
-
if ( $modes[‘underlined’] )
-
$resultString .= ‘<u>’;
-
-
$i++;
-
break;
-
// Color — Control+K
-
case ‘’:
-
if ( $modes[‘bold’] )
-
$resultString .= ‘</strong>’;
-
if ( $modes[‘underlined’] )
-
$resultString .= ‘</u>’;
-
-
if ( !$modes[‘reverse’] && ($modes[‘currentBG’] || $modes[‘currentFG’]) )
-
$resultString .= ‘</span>’;
-
-
if ( isset($string[$i+1]) && ctype_digit($string[$i+1]) ) //1
-
{
-
if ( isset($string[$i+2]) && ctype_digit($string[$i+2]) ) //12
-
{
-
if ( isset($string[$i+3]) && $string[$i+3] == ‘,’ ) //12,
-
{
-
if ( isset($string[$i+4]) && ctype_digit($string[$i+4]) ) //12,1
-
{
-
$number1 = (int)($string[$i+1].$string[$i+2]);
-
$number2 = 0;
-
-
if ( isset($string[$i+5]) && ctype_digit($string[$i+5]) ) //12,12
-
{
-
$number2 = (int)($string[$i+4].$string[$i+5]);
-
$i += 6;
-
}
-
else //12,1
-
{
-
$number2 = (int)($string[$i+4]);
-
$i += 5;
-
}
-
-
if ( isset($colors[$number1]) )
-
$modes[‘currentFG’] = $colors[$number1];
-
else
-
$modes[‘currentFG’] = »;
-
if ( isset($colors[$number2]) )
-
$modes[‘currentBG’] = $colors[$number2];
-
else
-
$modes[‘currentBG’] = »;
-
}
-
else //12
-
{
-
$number = (int)($string[$i+1].$string[$i+2]);
-
if ( isset($colors[$number]) )
-
$modes[‘currentFG’] = $colors[$number];
-
else
-
$modes[‘currentFG’] = »;
-
$i += 3;
-
}
-
}
-
else //12
-
{
-
$number = (int)($string[$i+1].$string[$i+2]);
-
if ( isset($colors[$number]) )
-
$modes[‘currentFG’] = $colors[$number];
-
else
-
$modes[‘currentFG’] = »;
-
$i += 3;
-
}
-
}
-
else if ( isset($string[$i+2]) && $string[$i+2] == ‘,’ //1,
-
&& isset($string[$i+3]) && ctype_digit($string[$i+3]) )//1,1
-
{
-
$number1 = (int)($string[$i+1]);
-
$number2 = (int)($string[$i+3]);
-
if ( isset($string[$i+4]) && ctype_digit($string[$i+4]) ) //1,12
-
{
-
$number2 .= (int)($string[$i+4]);
-
$i += 5;
-
}
-
else
-
{
-
$i += 4;
-
}
-
if ( isset($colors[$number1]) )
-
$modes[‘currentFG’] = $colors[$number1];
-
else
-
$modes[‘currentFG’] = »;
-
if ( isset($colors[$number2]) )
-
$modes[‘currentBG’] = $colors[$number2];
-
else
-
$modes[‘currentBG’] = »;
-
}
-
else //1
-
{
-
$modes[‘currentFG’] = $colors[$string[$i+1]];
-
$i += 2;
-
}
-
-
if ( !$modes[‘reverse’] ) // paste to string later?
-
if ( $modes[‘currentBG’] || $modes[‘currentFG’] )
-
$resultString .= ‘<span style=»‘.($modes[‘currentFG’] ? «color:{$modes[‘currentFG’]};»:»).($modes[‘currentBG’] ? «background-color:{$modes[‘currentBG’]};»:»).‘»>’;
-
}
-
else //
-
{
-
$i++;
-
$modes[‘currentBG’] = $modes[‘currentFG’] = »;
-
}
-
-
if ( $modes[‘bold’] )
-
$resultString .= ‘<strong>’;
-
if ( $modes[‘underlined’] )
-
$resultString .= ‘<u>’;
-
break;
-
-
// Plain — Control+O
-
case ‘’:
-
if ( $modes[‘bold’] )
-
$resultString .= ‘</strong>’;
-
if ( $modes[‘underlined’] )
-
$resultString .= ‘</u>’;
-
if ( $modes[‘reverse’] || $modes[‘currentBG’] || $modes[‘currentFG’] )
-
$resultString .= ‘</span>’;
-
-
$modes[‘reverse’] = $modes[‘bold’] = $modes[‘underlined’] = false;
-
$modes[‘currentBG’] = $modes[‘currentFG’] = »;
-
$i++;
-
break;
-
// Tab char
-
case ‘ ‘:
-
$resultString .= ‘ ‘;
-
-
$i++;
-
break;
-
default:
-
$resultString .= $string[$i];
-
$i++;
-
}
-
}
-
-
return $resultString;
-
}
-
-
// examples
-
-
$ex2 = ‘2(121202:123402:124302) 02(02@12perfect`light02) 12 5,7colorreverce4,2 |color in recerceand of re85verceplain
-
02(121202:123502:121402) 02(02@12perfect`light02) 12 5,7colorreverce4,2 |color in recerceand of re85,85verceplain’;
-
-
$ex1 = ‘2(121302:122302:121102) 02 —›12 03join: 02(03SaZ02) (03SaZ@195.222.69.20502)
-
02(121302:122302:121802) 02 —›12 quit: 02(12SaZ02) (12SaZ@195.222.69.20502) 02(12Client Quit02)12
-
02(121302:122302:125002) 02 —›12 03join: 02(03Aragorn02) (03Aragorn@195.222.66.22002)’;
-
-
var_dump(ParseIRChat($ex1));
-
echo «<p>»;
-
var_dump(ParseIRChat($ex2));
-
-
?>