<?php
/*
:::::::::::::::::::::::::::::::::::::::::::::::::::::
 Qbbcode
:::::::::::::::::::::::::::::::::::::::::::::::::::::
*/
function qcode_bb($bbcode)
{
    $bbcode = strip_tags($bbcode);
	$bbcode = str_replace('<', '', $bbcode);
    $bbcode = str_replace('>', '', $bbcode);
    $bbcode = str_replace('$', '', $bbcode);
    $bbcode = str_replace(';', '', $bbcode);
    $bbcode = str_replace('"', '', $bbcode);
    $bbcode = str_replace('\'', '', $bbcode);
    
    $bbcode = preg_replace("#(\[b\])+#is","\\1", $bbcode);
    $bbcode = preg_replace("#(\[i\])+#is","\\1", $bbcode);
    $bbcode = preg_replace("#(\[u\])+#is","\\1", $bbcode);
    $bbcode = preg_replace("#(\[s\])+#is","\\1", $bbcode);
    $bbcode = preg_replace("#(\[sup\])+#is","\\1", $bbcode);
    $bbcode = preg_replace("#(\[sub\])+#is","\\1", $bbcode);
    
    //$bbcode = preg_replace("#\[br]#is","<br />",$bbcode);
    $bbcode = preg_replace("#\[\:(.+?)\:\]#is", "<img src=\"theme/img/smile/\\1.gif\" />", $bbcode);
	$bbcode = ereg_replace("(\r\n|\n|\r)", "<br />", $bbcode);
    $bbcode = preg_replace("#\[p\](.+?)\[/p\]#is","<p>\\1</p>",$bbcode);
    $bbcode = preg_replace("#\[b\](.+?)\[/b\]#is","<strong>\\1</strong>",$bbcode);
    $bbcode = preg_replace("#\[i\](.+?)\[/i\]#is","<em>\\1</em>",$bbcode);
    $bbcode = preg_replace("#\[u\](.+?)\[/u\]#is","<u>\\1</u>",$bbcode);
    $bbcode = preg_replace("#\[s\](.+?)\[/s\]#is","<s>\\1</s>",$bbcode);
    $bbcode = preg_replace("#\[sup\](.+?)\[/sup\]#is","<sup>\\1</sup>",$bbcode);
    $bbcode = preg_replace("#\[sub\](.+?)\[/sub\]#is","<sub>\\1</sub>",$bbcode);
    $bbcode = preg_replace("#\[list=(1|a|I)\](.+?)\[/list\]#is","<ol type=\"\\1\">\\2</ol>",$bbcode);
    $bbcode = preg_replace("#\[\*\](.*?)\[/\*\]#is","<li>\\1</li>",$bbcode);
    $bbcode = preg_replace("#\[url=(.+?)\](.+?)\[\/url\]#is","<a href=\"\\1\" target=\"_blank\">\\2</a>",$bbcode);
    $bbcode = preg_replace("#\[img=(.+?)\](.+?)\[\/img\]#is","<img src=\"\\1\" alt=\"\\2\" border=\"0\" />",$bbcode);
    $bbcode = preg_replace("#\[php\](.+?)\[/php\]#is","<div><pre class=\"php\">PHP<br />\\1</pre></div>",$bbcode);
    $bbcode = preg_replace("#\[js\](.+?)\[/js\]#is","<div><pre class=\"js\">JS<br />\\1</pre></div>",$bbcode);
    $bbcode = preg_replace("#\[msg=(.+?)\](.+?)\[\/msg\]#is","<div id=\"msgclose\" name=\"\\1\">\\2</div>",$bbcode);
    return $bbcode;
}

function qcode_html($htmlcode)
{
    $htmlcode = preg_replace("#<img src=\"theme/img/smile/(\d+).gif\" />#is", "[:\\1:]", $htmlcode);
    //$htmlcode = preg_replace("#<br />#is","[br]",$htmlcode);
    $htmlcode = preg_replace("#<br />#is","\r\n",$htmlcode);
    $htmlcode = preg_replace("#<p>(.+?)</p>#is","[p]\\1[/p]",$htmlcode);
    $htmlcode = preg_replace("#<strong>(.+?)</strong>#is","[b]\\1[/b]",$htmlcode);
    $htmlcode = preg_replace("#<i>(.+?)</i>#is","[em]\\1[/em]",$htmlcode);
    $htmlcode = preg_replace("#<u>(.+?)</u>#is","[u]\\1[/u]",$htmlcode);
    $htmlcode = preg_replace("#<s>(.+?)</s>#is","[s]\\1[/s]",$htmlcode);
    $htmlcode = preg_replace("#<sup>(.+?)</sup>#is","[sup]\\1[/sup]",$htmlcode);
    $htmlcode = preg_replace("#<sub>(.+?)</sub>#is","[sub]\\1[/sub]",$htmlcode);
    $htmlcode = preg_replace("#<ul>(.+?)</ul>#is","[list]\\1[/list]",$htmlcode);
    $htmlcode = preg_replace("#<ol type=\"(.+?)\">(.+?)</ol>#is","[list=\\1]\\2[/list]",$htmlcode);
    $htmlcode = preg_replace("#<li>(.*?)</li>#is","[*]\\1[/*]",$htmlcode);
    $htmlcode = preg_replace("#<a href=\"(.+?)\" target=\"\_blank\">(.+?)</a>#is","[url=\\1]\\2[/url]",$htmlcode);
    $htmlcode = preg_replace("#<img src=\"(.+?)\" alt=\"(.+?)\" border=\"0\" />#is","[img=\\1]\\2[/img]",$htmlcode);
    $htmlcode = preg_replace("#<fieldset\s(.+?)><legend\s(.+?)>(.+?) писал\(а\)</legend>(.+?)</fieldset>#is","[quote=\\3]\\4[/quote]",$htmlcode);
    $htmlcode = preg_replace("#<fieldset\s(.+?)><legend\s(.+?)>Цитирование</legend>(.+?)</fieldset>#is","[quote]\\3[/quote]",$htmlcode);
    $htmlcode = preg_replace("#<div><pre class=\"php\">PHP<br />(.+?)</pre></div>#is","[php]\\1[/php]",$htmlcode);
    $htmlcode = preg_replace("#<div><pre class=\"js\">JS<br />(.+?)</pre></div>#is","[js]\\1[/js]",$htmlcode);
    $htmlcode = preg_replace("#<div id=\"msgclose\" name=\"(.+?)\">(.+?)</div>#is","[msg=\\1]\\2[/msg]",$htmlcode);
    return $htmlcode;
}

?>