<?php
function bbcode($string)
{ 
    //No HTML
    $string = trim(str_replace("<", "<", $string));
    $string = trim(str_replace(">", ">", $string));
    //CHR 13
    $string = trim(str_replace(chr(13), "<br>;", $string));
    $string = trim(str_replace(";", "", $string));
    //BBCODE
    $string = trim(str_replace("[B]", "<b>", $string));
    $string = trim(str_replace("[/B]", "</b>", $string));
    $string = trim(str_replace("[I]", "<i>", $string));
    $string = trim(str_replace("[/I]", "</i>", $string));
    $string = trim(str_replace("[U]", "<u>", $string));
    $string = trim(str_replace("[/U]", "</u>", $string));
    $string = trim(str_replace("[HR]", "<hr>", $string));
    $string = trim(str_replace("[LI]", "<li>", $string));
    $string = trim(str_replace("[IMG=", "<img border='0' src='", $string));
    $string = trim(str_replace("[/IMG]", "'>", $string)); 
    $string = trim(str_replace("[URL=", "<a href='", $string));
    $string = trim(str_replace("[/URL]", "</a>", $string));
    $string = trim(str_replace("[ALIGN=left]", "<div align='left'>", $string));
    $string = trim(str_replace("[ALIGN=center]", "<div align='center'>", $string));
    $string = trim(str_replace("[ALIGN=right]", "<div align='right'>", $string));
    $string = trim(str_replace("[/ALIGN]", "</div>", $string));
    $string = trim(str_replace("[EMAIL=", "<a href=mailto:", $string));
    $string = trim(str_replace("[/EMAIL]", "</a>", $string));
    $string = trim(str_replace("[COLOR=", "<font color=", $string));
    $string = trim(str_replace("[/COLOR]", "</font>", $string));
    return $string;
}
function bbcode_more($stringa, $id){
    //var_dump(strpos("[/MORE]",$stringa)); echo " strpos<br>";
	//var_dump(strpos($stringa,"[/MORE]")); echo " strpos<br>";
	//if(strpos("[/MORE]",$stringa)){//invertito 
	if(strpos($stringa,"[/MORE]")){
        $str=explode("[/MORE]",$stringa);
        if(trim($str[0]) !="[MORE]"){//la stringa[0] è formata da [MORE] + qualcos'altro
            $dopo = trim(str_replace($str[0], " <a href='index.php?m=news_view&id=$id'>read more</a>", $str[0]));  
            $prima=str_replace("[MORE]","",$str[0]);
            //Ritorno il valore
            return $prima."... ".$dopo;
        }else{//la stringa[0] è formata solo da [MORE]
            $stringa=str_replace("[MORE]","",$stringa);
			$stringa=str_replace("[/MORE]","",$stringa);
			return $stringa;
        }
    }else{
        return $stringa;//se non c'è [MORE] deve tornare la stringa
    }
} 
function bbcode_complete($string)
{ 
    //No HTML
    $string = trim(str_replace("<", "<", $string));
    $string = trim(str_replace(">", ">", $string));
    //CHR 13
    $string = trim(str_replace(chr(13), "<br>;", $string));
    $string = trim(str_replace(";", "", $string));
    //BBCODE
    $string = trim(str_replace("[B]", "<b>", $string));
    $string = trim(str_replace("[/B]", "</b>", $string));
    $string = trim(str_replace("[I]", "<i>", $string));
    $string = trim(str_replace("[/I]", "</i>", $string));
    $string = trim(str_replace("[U]", "<u>", $string));
    $string = trim(str_replace("[/U]", "</u>", $string));
    $string = trim(str_replace("[HR]", "<hr>", $string));
    $string = trim(str_replace("[LI]", "<li>", $string));
    $string = trim(str_replace("[IMG=", "<img border='0' src='", $string));
    $string = trim(str_replace("[/IMG]", "'>", $string)); 
    $string = trim(str_replace("[URL=", "<a href='", $string));
    $string = trim(str_replace("[/URL]", "</a>", $string));
    $string = trim(str_replace("[ALIGN=left]", "<div align='left'>", $string));
    $string = trim(str_replace("[ALIGN=center]", "<div align='center'>", $string));
    $string = trim(str_replace("[ALIGN=right]", "<div align='right'>", $string));
    $string = trim(str_replace("[/ALIGN]", "</div>", $string));
    $string = trim(str_replace("[EMAIL=", "<a href=mailto:", $string));
    $string = trim(str_replace("[/EMAIL]", "</a>", $string));
    $string = trim(str_replace("[COLOR=", "<font color=", $string));
    $string = trim(str_replace("[/COLOR]", "</font>", $string));
	$string = trim(str_replace("[MORE]", "", $string));
    $string = trim(str_replace("[/MORE]", "", $string));
    //Ritorno il valore
    return $string;
}
//---------------
$row[1]="[MORE]nel [B]mezzo[/B] di[/MORE] camin di nostra vita";
$row[2]="mi ritrovai in ina selva oscura che [I]la [/I]diritta via era smarrita";
$row[3]="pape satan pape satan[MORE] aleppe grido[/MORE] pluto [U]con la voce chioccia[/U]";
$row[4]="[MORE][/MORE]voci alte e fioce e suon di man con elle";
//originali
foreach($row as $ch => $val){
	 echo "<p>$ch) $val</p>";
}
echo "<hr>";
//prova  read more
foreach($row as $ch => $val){
	 echo "<p>".bbcode_more(bbcode($val), $ch)."</p>";
}
echo "<hr>";
//simulazione news completa
foreach($row as $ch => $val){
	 echo "<p>$ch) ".bbcode_complete($val)."</p>";
}
?>