<?
function bbcod($input, $sql=false, $bbc=false) {
    $input = htmlspecialchars($input, ENT_QUOTES,'WINDOWS-1251');

    if(get_magic_quotes_gpc ())
    {
        $input = stripslashes ($input);
    }
    if ($sql)
    {
        $input = mysql_real_escape_string ($input);
    }
    $input = strip_tags($input);

    if ($sql)
    {
        $input=str_replace ("\\r\\n\\r\\n\\r\\n\\r\\n","<br><br>", $input);
        $input=str_replace ("\\r\\n\\r\\n\\r\\n","<br><br>", $input);
        $input=str_replace ("\\r\\n\\r\\n","<br><br>", $input);
        $input=str_replace ("\\r\\n","<br>", $input);
        $input=str_replace ("\\n"," ", $input);
        $input=str_replace ("\\r","", $input);
    }else{
    	$input=str_replace ("\n"," ", $input);
        $input=str_replace ("\r","", $input);
        $input=str_replace ("\r\n","<br>", $input);
    }

    if($bbc){
       $find = array ("'\[b\]'i", "'\[/b\]'i", "'\[i\]'i", "'\[/i\]'i", "'\[u\]'i", "'\[/u\]'i");

	   $replace = array ("<b>", "</b>", "<i>", "</i>", "<u>", "</u>");

      $input = preg_replace( $find, $replace, $input );



      $cdom = "http://".$_SERVER['HTTP_HOST']."/";
      $input = str_replace(":-_","<img src=\"".$cdom."copilca/smile/1.gif\" border=\"0\" alt=\"\">",$input);
      $input = str_replace(":-8","<img src=\"".$cdom."copilca/smile/2.gif\" border=\"0\" alt=\"\">",$input);
      $input = str_replace(":-(","<img src=\"".$cdom."copilca/smile/3.gif\" border=\"0\" alt=\"\">",$input);
      $input = str_replace(":~)","<img src=\"".$cdom."copilca/smile/4.gif\" border=\"0\" alt=\"\">",$input);
      $input = str_replace(":-9","<img src=\"".$cdom."copilca/smile/5.gif\" border=\"0\" alt=\"\">",$input);
      $input = str_replace("8-)","<img src=\"".$cdom."copilca/smile/6.gif\" border=\"0\" alt=\"\">",$input);
      $input = str_replace("9-)","<img src=\"".$cdom."copilca/smile/7.gif\" border=\"0\" alt=\"\">",$input);
      $input = str_replace("10-)","<img src=\"".$cdom."copilca/smile/8.gif\" border=\"0\" alt=\"\">",$input);
      $input = str_replace("11-)","<img src=\"".$cdom."copilca/smile/9.gif\" border=\"0\" alt=\"\">",$input);
      $input = str_replace("12-)","<img src=\"".$cdom."copilca/smile/10.gif\" border=\"0\" alt=\"\">",$input);
      $input = str_replace("13-)","<img src=\"".$cdom."copilca/smile/11.gif\" border=\"0\" alt=\"\">",$input);
    }

    return $input;
}

function bbb($input){

 $input=str_replace ("<br><br>","\r\n\r\n", $input);
 $input=str_replace ("<br>","\r\n", $input);

 $find = array ("[b]", "[/b]", "[i]", "[/i]", "[u]", "[/u]");

 $replace = array ("'<b>'i", "'</b>'i", "'<i>'i", "'</i>'i", "'<u>'i", "'</u>'i");

 $input = preg_replace($replace, $find, $input);

 $cdom = "http://".$_SERVER['HTTP_HOST']."/";
 $input = str_replace("<img src=\"".$cdom."copilca/smile/1.gif\" border=\"0\" alt=\"\">",":-_",$input);
 $input = str_replace("<img src=\"".$cdom."copilca/smile/2.gif\" border=\"0\" alt=\"\">",":-8",$input);
 $input = str_replace("<img src=\"".$cdom."copilca/smile/3.gif\" border=\"0\" alt=\"\">",":-(",$input);
 $input = str_replace("<img src=\"".$cdom."copilca/smile/4.gif\" border=\"0\" alt=\"\">",":~)",$input);
 $input = str_replace("<img src=\"".$cdom."copilca/smile/5.gif\" border=\"0\" alt=\"\">",":-9",$input);
 $input = str_replace("<img src=\"".$cdom."copilca/smile/6.gif\" border=\"0\" alt=\"\">","8-)",$input);
 $input = str_replace("<img src=\"".$cdom."copilca/smile/7.gif\" border=\"0\" alt=\"\">","9-)",$input);
 $input = str_replace("<img src=\"".$cdom."copilca/smile/8.gif\" border=\"0\" alt=\"\">","10-)",$input);
 $input = str_replace("<img src=\"".$cdom."copilca/smile/9.gif\" border=\"0\" alt=\"\">","11-)",$input);
 $input = str_replace("<img src=\"".$cdom."copilca/smile/10.gif\" border=\"0\" alt=\"\">","12-)",$input);
 $input = str_replace("<img src=\"".$cdom."copilca/smile/11.gif\" border=\"0\" alt=\"\">","13-)",$input);

 return $input;
}
?>