scusate ho provato ma se metto la stringa html mi converte tutto

codice:
<?php 
//Funzione per interpretare il testo 
function fBBCode($string) 
{ 
     //No HTML 
     $string = trim(str_replace("<", "<", $string)); 
     $string = trim(str_replace(">", ">", $string)); 
      
     //CHR 13 
     $string = trim(str_replace(chr(13), "
;", $string)); 
     $string = trim(str_replace(";", "", $string)); 
      
     //BBCODE 
     $string = trim(str_replace("", "", $string)); 
     $string = trim(str_replace("", "", $string)); 
     $string = trim(str_replace("[C]", "", $string)); 
     $string = trim(str_replace("[/C]", "", $string)); 
     $string = trim(str_replace("[S]", "<u>", $string)); 
     $string = trim(str_replace("[/S]", "</u>", $string)); 
     $string = trim(str_replace("[HR]", "<hr>", $string)); 
     $string = trim(str_replace("[LI]", "[*]", $string)); 
     $string = trim(str_replace("[/LI]", "", $string)); 
     $string = trim(str_replace("[IMG=", "<img border='0' src='", $string)); 
     $string = trim(str_replace("[URL=", "<a target='_blank' href='", $string)); 
     $string = trim(str_replace("[/URL]", "</a>", $string)); 
     $string = trim(str_replace("[MAILTO=", "<a href='mailto:", $string));
     $string = trim(str_replace("[/MAILTO]", "</a>", $string)); 
     $string = trim(str_replace("[LEFT]", "<div align='left'>", $string)); 
     $string = trim(str_replace("[CENTER]", "<div align='center'>", $string)); 
     $string = trim(str_replace("[RIGHT]", "<div align='right'>", $string)); 
     $string = trim(str_replace("[/LEFT]", "</div>", $string)); 
     $string = trim(str_replace("[/CENTER]", "</div>", $string)); 
     $string = trim(str_replace("[/RIGHT]", "</div>", $string)); 
     $string = trim(str_replace("[COLOR=", "<font color='", $string)); 
     $string = trim(str_replace("[/COLOR]", "</font>", $string));
	 $string = trim(str_replace("]CODE[", ">pre<", $string));
	 $string = trim(str_replace("]/CODE[", ">/pre<", $string));  
	 $string = trim(str_replace("]", "'>", $string)); 
      
     //Ritorno il valore 
     return $string; 
	 } 
?>
ora nn saprei propio come piazzare
$messaggio= htmlentities($messaggio);
e come piazzarlo