N'ayant pas trouvé (et pas cherché) une fonction pour transformer lee bbcode en html j'ai créé ma propre fonction.
Je vous la présente.
Désolé il y a très peu de commentaires.
Avec cette version je gère tous les bbcodes utilisés sur le forum actuellement. (je n'ai pas trouvé de bbcodes non implémenté).
Cliquez pour révéler
Cliquez pour masquer
<?php
function splitmagic($pattern,&$var,$pf,$rf,$pd,$rd1,$rd2,$taille,&$nb)//passage en reference
{
$temp2 = preg_split($pattern, $var);
$nb = count($temp2);
if ($nb > 1)
{
$coul = substr($temp2[1],0,$taille);
$var = preg_replace($pf,$rf,$var);
$var = preg_replace($pd,$rd1.$coul.$rd2,$var,1);
}
}
function accents($texte)
{
//$accent = array("//","'","/°/","°","/
/","...","/|\/","\"","//","œ", "//","'","/\\n/","<br />","/à/","à","/À/","À","/á/","á","/Á/","Á","/â/","â","/Â/","Â","/ã/","ã","/Ã/","Ã","/ä/","ä","/Ä/","Ä","/å/","å","/Å/","Å","/æ/","æ","/Æ/","Æ","/è/","è","/È/","È","/é/","é","/É/","É","/ê/","ê","/Ê/","Ê","/ë/","ë","/Ë/","Ë","/ì/","ì","/Ì/","Ì","/í/","í","/Í/","Í","/î/","î","/Î/","Î","/ï/","ï","/Ï/","Ï","/ò/","ò","/Ò/","Ò","/ó/","ó","/Ó/","Ó","/ô/","ô","/Ô/","Ô","/õ/","õ","/Õ/","Õ","/ö/","ö","/Ö/","Ö","/ø/","ø","/Ø/","Ø","/ù/","ù","/Ù/","Ù","/ú/","ú","/Ú/","Ú","/û/","û","/Û/","Û","/ü/","ü","/Ü/","Ü","/ñ/","ñ","/Ñ/","Ñ","/ç/","ç","/Ç/","Ç","/ý/","ý","/Ý/","Ý","/ß/","ß","/«/","«","/»/","»","/§/","¶","/©/","©");
//plus besoin des accents
$temp = $texte;
$temp = preg_replace("/\\n/","<br />",$temp);//retour à la ligne
/*for ($i = 0; $i < count($accent); $i=$i+2)
{
$temp = preg_replace($accent[$i],$accent[$i+1],$temp);
}*/
$temp = preg_replace("/{SMILIES_PATH}/","../images/smilies",$temp);//smilies
//------------------------------couleur---------------------------------------------------
$nbv= 0;
splitmagic("/\[color=#/",$temp,"/\[\/color:[A-Za-z0-9]+\]+/","</span>","/\[color=#[A-Za-z0-9]+:[A-Za-z0-9]+\]/","<span style=\"color: #","\">",6,$nbv);
while ($nbv > 1)
{
splitmagic("/\[color=#/",$temp,"/\[\/color:[A-Za-z0-9]+\]/","</span>","/\[color=#[A-Za-z0-9]+:[A-Za-z0-9]+\]/","<span style=\"color: #","\">",6,$nbv);
}
$cptc = strpos($temp,"[color=");
if ($cptc)
{
$temp = preg_replace("/\[\/color:[A-Za-z0-9]+\]+/","</span>",$temp);//fin de </a>
while ($cptc = strpos($temp,"[color="))
{
$deb = strpos($temp,"=",$cptc);
$fin = strpos($temp,":",$deb+1);
$couleur = substr($temp,$deb+1,$fin-($deb+1));
$temp = preg_replace("/\[color=[A-Za-z0-9]+:[A-Za-z0-9]+\]/","<span style=\"color: ".$couleur."\">",$temp,1);//<span style="color: $couleur">
}
}
//------------------------------/couleur---------------------------------------------------
//------------------------------size---------------------------------------------------
$nbv= 0;
splitmagic("/\+\]/","</span>","/\+:[A-Za-z0-9]+\]/","<span style=\"font-size: ","%; line-height: normal\">",3,$nbv);
while ($nbv > 1)
{
splitmagic("/\+\]/","</span>","/\+:[A-Za-z0-9]+\]/","<span style=\"font-size: ","%; line-height: normal\">",3,$nbv);
}
//------------------------------/size---------------------------------------------------
$temp = preg_replace("/\[\/tab:[A-Za-z0-9]+\]/","</span>",$temp);//</span>début tab
$temp = preg_replace("/\[tab:[A-Za-z0-9]+\]/","<span style=\"margin-left: 35px;\">",$temp);//<span>fin tab
$temp = preg_replace("/\[\/quote:[A-Za-z0-9]+\]/","</div>",$temp);//</div>quote
$temp = preg_replace("/\[quote:[A-Za-z0-9]+\]/","<div class=\"quotetitle\"><b>Citer:</b></div><div class=\"quotecontent\">",$temp);//<div>quote
$temp = preg_replace("/\[\/list:u:[A-Za-z0-9]+\]/","</ul>",$temp);//</ul>
$temp = preg_replace("/\[list:[A-Za-z0-9]+\]/","<ul>",$temp);//<ul>
$temp = preg_replace("/\[\/\*:m:[A-Za-z0-9]+\]/","</li>",$temp);//</li>
$temp = preg_replace("/\[\*:[A-Za-z0-9]+\]/","<li>",$temp);//<li>
$temp = preg_replace("/\[\/img:[A-Za-z0-9]+\]/","\" alt=\"Image\" />",$temp);//" alt="Image" />
$temp = preg_replace("/\[img:[A-Za-z0-9]+\]/","<img src=\"",$temp);//<img src="
$temp = preg_replace("/\[\/code:[A-Za-z0-9]+\]/","</div>",$temp);//</div> code
$temp = preg_replace("/\[code:[A-Za-z0-9]+\]/","<div class=\"codetitle\"><b>Code:</b></div><div class=\"codecontent\">",$temp);//<div> code
$temp = preg_replace("/\[\/spoiler:[A-Za-z0-9]+\]/","</div></div></div>",$temp);// fin spoiler
$temp = preg_replace("/\[spoiler:[A-Za-z0-9]+\]/","<div style=\"padding: 3px; background-color: #FFFFFF; border: 1px solid #d8d8d8; font-size: 1em;\"><div style=\"text-transform: uppercase; border-bottom: 1px solid #CCCCCC; margin-bottom: 3px; font-size: 0.8em; font-weight: bold; display: block;\"><span onClick=\"if (this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display != '') { this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display = ''; this.innerHTML = '<b>Spoiler: </b><a href=\'#\' onClick=\'return false;\'>hide</a>'; } else { this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display = 'none'; this.innerHTML = '<b>Spoiler: </b><a href=\'#\' onClick=\'return false;\'>show</a>'; }\" /><b>Spoiler: </b><a href=\"#\" onClick=\"return false;\">show</a></span></div><div class=\"quotecontent\"><div style=\"display: none;\">",$temp);//début spoiler
$temp = preg_replace("/\[\/i:[A-Za-z0-9]+\]/","</em>",$temp);//fin d'italique
$temp = preg_replace("/\[i:[A-Za-z0-9]+\]/","<em>",$temp);//début d'italique
$temp = preg_replace("/\[\/u:[A-Za-z0-9]+\]/","</span>",$temp);//fin souligné
$temp = preg_replace("/\[u:[A-Za-z0-9]+\]/","<span style=\"text-decoration: underline\">",$temp);//début souligné
$temp = preg_replace("/\[\/centrer:[A-Za-z0-9]+\]/","</div>",$temp);//fin centré
$temp = preg_replace("/\[centrer:[A-Za-z0-9]+\]/","<div style=\"text-align: center;\">",$temp);//début centré
$cpt = strpos($temp,"[url:");
$temp = preg_replace("/\[\/url:[A-Za-z0-9]+\]/","</a>",$temp);//fin de </a>
if ($cpt == 0)
{
$temp = preg_replace("/\[url=/","<a href=\"",$temp);//lien début <a href=\"
}
else
{
while ($cpt = strpos($temp,"[url:"))
{
$deb = strpos($temp,"]",$cpt);
$fin = strpos($temp,"<",$deb+1);
$url = substr($temp,$deb+1,$fin-($deb+1));
$temp = preg_replace("/\[url:[A-Za-z0-9]+\]/","<a href=\"".$url."\" class=\"postlink\">",$temp,1);//lien début <a href="url">
$temp = preg_replace("/\[url=/","<a href=\"",$temp);//lien début <a href=\"
}
}
$temp = preg_replace("/\[\/b:[A-Za-z0-9]+\]/","</strong>",$temp);//</strong>
$temp = preg_replace("/\[b:[A-Za-z0-9]+\]/","<strong>",$temp);//<strong>
$temp = preg_replace("/:[A-Za-z0-9]+\]/","\" class=\"postlink\">",$temp);//lien" class="">
return $temp;
}
?>