Конвертер форматированного текста из mIRC в HTML (IRC2HTML)

PHP-скрипт
Сегодня я написал функцию-конвертер форматированного текста, скопированного из окна (или другого IRC-клиента) в -представление. Назвал функцию ParseIRChat, хотя подходящие названия mIRC2HTML, irc to html и т.д. Функция принимает текст из чата и возвращает HTML-код. Написана на . Открыта как обычно под лицензией BSD.

Версия 1.0.0.0 beta.

Итак, приведу ниже код функции. Скачать PHP-файл можно здесь: mircChat2html.zip, mircChat2html.tar.gz. А online demo можно посмотреть тут.

 

 

  1. <?php

  2.  

  3. /**

  4. * mIRC-Chat text highlighter.

  5. * Get mIRC-formatted string and returnes in HTML-format

  6. * Color scheme is near mIRC default (light scheme)

  7. *

  8. * @param string $string

  9. * @return string

  10. */

  11. function ParseIRChat($string)

  12. {

  13.  

  14. $colors = array(

  15. 0 => ‘white’,

  16. 1 => ‘black’,

  17. 2 => ‘blue’,

  18. 3 => ‘green’,

  19. 4 => ‘#FA5A5A’, //lightred

  20. 5 => ‘brown’,

  21. 6 => ‘purple’,

  22. 7 => ‘orange’,

  23. 8 => ‘yellow’,

  24. 9 => ‘lightgreen’,

  25. 10 => ‘cyan’,

  26. 11 => ‘lightcyan’,

  27. 12 => ‘lightblue’,

  28. 13 => ‘pink’,

  29. 14 => ‘grey’,

  30. 15 => ‘lightgrey’

  31. );

  32.  

  33. $modes = array(

  34. ‘bold’ => false,

  35. ‘underlined’ => false,

  36. ‘reverse’ => false,

  37. ‘currentFG’ => »,

  38. ‘currentBG’ => »,

  39. );

  40.  

  41. $string = nl2br($string) . ‘’;

  42.  

  43. $resultString = »;

  44. $i = 0;

  45. while ( $i < strlen($string) )

  46. {

  47. switch ( $string[$i] )

  48. {

  49. // Bold — Control+B

  50. case ‘’:

  51. if ( $modes[‘underlined’] )

  52. $resultString .= ‘</u>’;

  53.  

  54. if ( $modes[‘bold’] = !$modes[‘bold’] )

  55. $resultString .= ‘<strong>’;

  56. else

  57. $resultString .= ‘</strong>’;

  58.  

  59. if ( $modes[‘underlined’] )

  60. $resultString .= ‘<u>’;

  61. $i++;

  62. break;

  63. // Undelined — Control+U

  64. case ‘’:

  65. if ( $modes[‘underlined’] )

  66. $resultString .= ‘</bold>’;

  67.  

  68. if ( !($modes[‘underlined’] = !$modes[‘underlined’]) )

  69. $resultString .= ‘</u>’;

  70. else

  71. $resultString .= ‘<u>’;

  72.  

  73. if ( $modes[‘underlined’] )

  74. $resultString .= ‘<bold>’;

  75. $i++;

  76. break;

  77. // Reverse — Control+R

  78. case ‘’:

  79. if ( $modes[‘bold’] )

  80. $resultString .= ‘</strong>’;

  81. if ( $modes[‘underlined’] )

  82. $resultString .= ‘</u>’;

  83.  

  84. if ( $modes[‘reverse’] = !$modes[‘reverse’] )

  85. {

  86. if ( $modes[‘currentBG’] || $modes[‘currentFG’] )

  87. $resultString .= ‘</span>’;

  88. $resultString .= ‘<span style=»color:’.$colors[0].‘;background-color:’.$colors[1].‘;»>’;

  89. }

  90. else

  91. {

  92. $resultString .= ‘</span>’;

  93. if ( $modes[‘currentBG’] || $modes[‘currentFG’] )

  94. $resultString .= ‘<span style=»‘.($modes[‘currentFG’] ? «color:{$modes[‘currentFG’]};»:»).($modes[‘currentBG’] ? «background-color:{$modes[‘currentBG’]};»:»).‘»>’;

  95. }

  96.  

  97. if ( $modes[‘bold’] )

  98. $resultString .= ‘<strong>’;

  99. if ( $modes[‘underlined’] )

  100. $resultString .= ‘<u>’;

  101.  

  102. $i++;

  103. break;

  104. // Color — Control+K

  105. case ‘’:

  106. if ( $modes[‘bold’] )

  107. $resultString .= ‘</strong>’;

  108. if ( $modes[‘underlined’] )

  109. $resultString .= ‘</u>’;

  110.  

  111. if ( !$modes[‘reverse’] && ($modes[‘currentBG’] || $modes[‘currentFG’]) )

  112. $resultString .= ‘</span>’;

  113.  

  114. if ( isset($string[$i+1]) && ctype_digit($string[$i+1]) ) //1

  115. {

  116. if ( isset($string[$i+2]) && ctype_digit($string[$i+2]) ) //12

  117. {

  118. if ( isset($string[$i+3]) && $string[$i+3] == ‘,’ ) //12,

  119. {

  120. if ( isset($string[$i+4]) && ctype_digit($string[$i+4]) ) //12,1

  121. {

  122. $number1 = (int)($string[$i+1].$string[$i+2]);

  123. $number2 = 0;

  124.  

  125. if ( isset($string[$i+5]) && ctype_digit($string[$i+5]) ) //12,12

  126. {

  127. $number2 = (int)($string[$i+4].$string[$i+5]);

  128. $i += 6;

  129. }

  130. else //12,1

  131. {

  132. $number2 = (int)($string[$i+4]);

  133. $i += 5;

  134. }

  135.  

  136. if ( isset($colors[$number1]) )

  137. $modes[‘currentFG’] = $colors[$number1];

  138. else

  139. $modes[‘currentFG’] = »;

  140. if ( isset($colors[$number2]) )

  141. $modes[‘currentBG’] = $colors[$number2];

  142. else

  143. $modes[‘currentBG’] = »;

  144. }

  145. else //12

  146. {

  147. $number = (int)($string[$i+1].$string[$i+2]);

  148. if ( isset($colors[$number]) )

  149. $modes[‘currentFG’] = $colors[$number];

  150. else

  151. $modes[‘currentFG’] = »;

  152. $i += 3;

  153. }

  154. }

  155. else //12

  156. {

  157. $number = (int)($string[$i+1].$string[$i+2]);

  158. if ( isset($colors[$number]) )

  159. $modes[‘currentFG’] = $colors[$number];

  160. else

  161. $modes[‘currentFG’] = »;

  162. $i += 3;

  163. }

  164. }

  165. else if ( isset($string[$i+2]) && $string[$i+2] == ‘,’ //1,

  166. && isset($string[$i+3]) && ctype_digit($string[$i+3]) )//1,1

  167. {

  168. $number1 = (int)($string[$i+1]);

  169. $number2 = (int)($string[$i+3]);

  170. if ( isset($string[$i+4]) && ctype_digit($string[$i+4]) ) //1,12

  171. {

  172. $number2 .= (int)($string[$i+4]);

  173. $i += 5;

  174. }

  175. else

  176. {

  177. $i += 4;

  178. }

  179. if ( isset($colors[$number1]) )

  180. $modes[‘currentFG’] = $colors[$number1];

  181. else

  182. $modes[‘currentFG’] = »;

  183. if ( isset($colors[$number2]) )

  184. $modes[‘currentBG’] = $colors[$number2];

  185. else

  186. $modes[‘currentBG’] = »;

  187. }

  188. else //1

  189. {

  190. $modes[‘currentFG’] = $colors[$string[$i+1]];

  191. $i += 2;

  192. }

  193.  

  194. if ( !$modes[‘reverse’] ) // paste to string later?

  195. if ( $modes[‘currentBG’] || $modes[‘currentFG’] )

  196. $resultString .= ‘<span style=»‘.($modes[‘currentFG’] ? «color:{$modes[‘currentFG’]};»:»).($modes[‘currentBG’] ? «background-color:{$modes[‘currentBG’]};»:»).‘»>’;

  197. }

  198. else //

  199. {

  200. $i++;

  201. $modes[‘currentBG’] = $modes[‘currentFG’] = »;

  202. }

  203.  

  204. if ( $modes[‘bold’] )

  205. $resultString .= ‘<strong>’;

  206. if ( $modes[‘underlined’] )

  207. $resultString .= ‘<u>’;

  208. break;

  209.  

  210. // Plain — Control+O

  211. case ‘’:

  212. if ( $modes[‘bold’] )

  213. $resultString .= ‘</strong>’;

  214. if ( $modes[‘underlined’] )

  215. $resultString .= ‘</u>’;

  216. if ( $modes[‘reverse’] || $modes[‘currentBG’] || $modes[‘currentFG’] )

  217. $resultString .= ‘</span>’;

  218.  

  219. $modes[‘reverse’] = $modes[‘bold’] = $modes[‘underlined’] = false;

  220. $modes[‘currentBG’] = $modes[‘currentFG’] = »;

  221. $i++;

  222. break;

  223. // Tab char

  224. case ‘  ‘:

  225. $resultString .= ‘    ‘;

  226.  

  227. $i++;

  228. break;

  229. default:

  230. $resultString .= $string[$i];

  231. $i++;

  232. }

  233. }

  234.  

  235. return $resultString;

  236. }

  237.  

  238. // examples

  239.  

  240. $ex2 = ‘2(121202:123402:124302) 02(02@12perfect`light02) 12 5,7colorreverce4,2 |color in recerceand of re85verceplain

  241. 02(121202:123502:121402) 02(02@12perfect`light02) 12 5,7colorreverce4,2 |color in recerceand of re85,85verceplain’;

  242.  

  243. $ex1 = ‘2(121302:122302:121102)    02 —›12 03join: 02(03SaZ02) (03SaZ@195.222.69.20502)

  244. 02(121302:122302:121802)    02 —›12 quit: 02(12SaZ02) (12SaZ@195.222.69.20502) 02(12Client Quit02)12

  245. 02(121302:122302:125002)    02 —›12 03join: 02(03Aragorn02) (03Aragorn@195.222.66.22002)’;

  246.  

  247. var_dump(ParseIRChat($ex1));

  248. echo «<p>»;

  249. var_dump(ParseIRChat($ex2));

  250.  

  251. ?>