Fonction BBcode en PHP Fonction BBcode en PHP Comment programmer une fonction de type BBcode faisant grosso-modo ce que font les célèbres forums Bulletin Board, et que vous pouvez adapter selon vos besoins, voici de quoi démarrer pour vos forums.
Pour rappel, il s'agit d'interpréter un texte qui aura été passé à la moulinette de la fonction htmlspecialchars() afin de pouvoir effectuer une mise en forme de ce texte [gras, taille, couleur, liens, images, videos, smileys etc...] sans toutefois permettre à l'utilisateur de placer des tags HTML.
<?
function bbcode($text)
{
// Pour garder les espaces
$text = str_replace(' ',' ', stripslashes($text));
// Pour créer les sauts de ligne
$text = preg_replace("#n#i", '<br>', $text);
// Pour les images, on enlève http pour éviter d'y voir un lien
if (eregi('[ picture=', $text)) { $text = preg_replace("#[ picture=http://#i",'[ picture=', $text); }
// Pour les videos, on enlève http pour éviter d'y voir un lien et on transforme les liens Youtube en url menant directement à la vidéo
if (eregi('[ video=', $text)) { $text = preg_replace("#[ video=http://#i",'[ video=', $text); $text = preg_replace("#[ video=www.youtube.com/watch?v=#i",'[ video=www.youtube.com/v/', $text); }
// Pour les url classiques et hypertexte
if (eregi('[ /url]', $text)) { $text = preg_replace("#url=http#i",'url=hzzp', $text); $text = preg_replace("#url]http#i",'url]hzzp', $text); $array_test_beg = split('[ url',strtolower($text)) + split('[ URL',strtolower($text)); $array_test_end = split('[ /url]',strtolower($text)) + split('[ /URL]',strtolower($text)); if (count($array_test_beg) == count($array_test_end)) { if (eregi('[ url=', $text)) $text = preg_replace("#[ url=([ ^[ ]*) ?] ?([ ^]]*) ?[ /url]#i", '<a href="1" rel="nofollow" target="_blank" title="2">2</a>', $text); if (eregi('[ url]', $text)) $text = preg_replace("#[ url]([ ^[ ]*)[ /url]#i", '<a href="1" rel="nofollow" target="_blank">1</a>', $text); } }
// Pour les Url non entourées de tags
if (eregi('http://', $text)) { $text = str_replace("""," ++^^$$"",$text); $text = preg_replace("#([ n ])?http://([ a-z0-9-=_%#$~%&;?./()+]+)#i", '1<a href="http://2" rel="nofollow" target="_blank">http://2</a>', $text); $text = str_replace(" ++^^$$"",""",$text); }
// Pour les Url classiques, reformatage
if (eregi('hzzp', $text)) { $text = preg_replace("#hzzp#i",'http', $text); }
if (eregi('[ ', $text))
{
// Pour les couleurs
if (eregi('[ /color]', $text)) { $array_test_beg = split('[ color=',strtolower($text)) + split('[ COLOR=',strtolower($text)); $array_test_end = split('[ /color]',strtolower($text)) + split('[ /COLOR]',strtolower($text)); if (count($array_test_beg) == count($array_test_end)) { $text = preg_replace("#[ color=red]#i", '<font color="#FF0000">', $text); $text = preg_replace("#[ color=magenta]#i", '<font color="#FF00FF">', $text); $text = preg_replace("#[ color=brown]#i", '<font color="#AA0000">', $text); $text = preg_replace("#[ /color]#i", '</font>', $text); } }
// Pour les tailles
if (eregi('[ /size]', $text)) { $array_test_beg = split('[ size=',strtolower($text)) + split('[ SIZE=',strtolower($text)); $array_test_end = split('[ /size]',strtolower($text)) + split('[ /SIZE]',strtolower($text)); if (count($array_test_beg) == count($array_test_end)) { $text = preg_replace("#[ size=([ 0-9])]([ ^]]*)[ /size]#i", '<font size="1">2</font>', $text); } }
// Pour les images
if (eregi('[ /picture]', $text)) { $array_test_beg = split('[ picture=',strtolower($text)) + split('[ PICTURE=',strtolower($text)); $array_test_end = split('[ /picture]',strtolower($text)) + split('[ /PICTURE]',strtolower($text)); if (count($array_test_beg) == count($array_test_end)) { if (eregi('[ picture=', $text)) $text = preg_replace("#[ picture=([ ^[ ]*) ?] ?([ ^]]*) ?[ /pic]#i", '<img src="http://1" alt="2">', $text); } }
// Pour les videos youtube (par exemple)
if (eregi('[ video=', $text)) { $text = preg_replace("#[ video=([ a-z0-9-=_%#$~%&;:?./]+)]#i", '<object width="425" height="350"><param name="movie" value="http://1"></param><param name="allowFullScreen" value="true"></param><param name="wmode" value="transparent"></param><embed src="http://1" type="application/x-shockwave-flash" wmode="transparent" width="425" height="350" allowFullScreen="true"></embed></object>', $text); }
// Pour les tableaux
if (eregi('[ /table]', $text)) { $array_test_beg = split('[ table',strtolower($text)) + split('[ TABLE',strtolower($text)); $array_test_end = split('[ /table]',strtolower($text)) + split('[ /TABLE]',strtolower($text)); if (count($array_test_beg) == count($array_test_end)) { $text = preg_replace("#[ table=([ 0-9]+)]#i", '<table width="1">', $text); $text = preg_replace("#[ table]#i",'<table>', $text); $text = preg_replace("#[ /table]#i",'</table>', $text); $text = preg_replace("#[ tr]#i",'<tr>', $text); $text = preg_replace("#[ /tr]#i",'</tr>', $text); $text = preg_replace("#[ td]#i",'<td>', $text); $text = preg_replace("#[ /td]#i",'</td>', $text); } }
// Pour les smilies, juste un exemple
if (eregi('[ :', $text)) { if (eregi('[ :)]',$text)) $text = preg_replace("#[ :)]#i", '<img src="/images/smilies/smile.gif">', $text); }
if (eregi('[ /b]', $text)) { $array_test_beg = split('[ b]',strtolower($text)) + split('[ B]',strtolower($text)); $array_test_end = split('[ /b]',strtolower($text)) + split('[ /B]',strtolower($text)); if (count($array_test_beg) == count($array_test_end)) { $text = preg_replace("#[ b]#i", '<b>', $text); $text = preg_replace("#[ /b]#i", '</b>', $text); } }
if (eregi('[ /i]', $text)) { $array_test_beg = split('[ i]',strtolower($text)) + split('[ I]',strtolower($text)); $array_test_end = split('[ /i]',strtolower($text)) + split('[ /I]',strtolower($text)); if (count($array_test_beg) == count($array_test_end)) { $text = preg_replace("#[ i]#i", '<i>', $text); $text = preg_replace("#[ /i]#i", '</i>', $text); } }
if (eregi('[ /u]', $text)) { $array_test_beg = split('[ u]',strtolower($text)) + split('[ U]',strtolower($text)); $array_test_end = split('[ /u]',strtolower($text)) + split('[ /U]',strtolower($text)); if (count($array_test_beg) == count($array_test_end)) { $text = preg_replace("#[ u]#i", '<u>', $text); $text = preg_replace("#[ /u]#i", '</u>', $text); } }
if (eregi('[ /strike]', $text)) { $array_test_beg = split('[ strike]',strtolower($text)) + split('[ STRIKE]',strtolower($text)); $array_test_end = split('[ /strike]',strtolower($text)) + split('[ /STRIKE]',strtolower($text)); if (count($array_test_beg) == count($array_test_end)) { $text = preg_replace("#[ strike]#i", '<strike>', $text); $text = preg_replace("#[ /strike]#i", '</strike>', $text); } }
if (eregi('[ /code]', $text)) { $array_test_beg = split('[ code]',strtolower($text)) + split('[ CODE]',strtolower($text)); $array_test_end = split('[ /code]',strtolower($text)) + split('[ /CODE]',strtolower($text)); if (count($array_test_beg) == count($array_test_end)) { $quote_color = '#FFE0B0'; $text = preg_replace("#[ code]#i",'<div style="color:#333333;background-color:'.$quote_color.';"><code>', $text); $text = preg_replace("#[ /code]#i",'</code></div>', $text); } }
if (eregi('[ /quote]', $text)) { $array_test_beg = split('[ quote]',strtolower($text)) + split('[ QUOTE]',strtolower($text)); $array_test_end = split('[ /quote]',strtolower($text)) + split('[ /QUOTE]',strtolower($text)); if (count($array_test_beg) == count($array_test_end)) { $text = preg_replace("#[ quote]#i",'<div style="background-color:#F0F0F0;"><blockquote><font size="-1">', $text); $text = preg_replace("#[ /quote]#i",'</font></blockquote></div>', $text); } }
if (eregi('[ /email]', $text)) { $text = preg_replace("#[ email]#i",'', $text); $text = preg_replace("#[ /email]#i",'', $text); }
}
// Pour les adresses email en format texte
if (eregi('@', $text)) { $text = preg_replace("#([ n ])?([ a-z0-9-_.]+)@([ a-z0-9-_.]+)#i", '12(at)3', $text); }
return($text);
}
function bbcode_title($text)
{
// Pour les sauts de ligne
$text = preg_replace("#([ nr]+)#i", ' ', stripslashes($text));
// Pour les tags
if (eregi('[ ', $text)) { $text = preg_replace("#[ (.[ ^]]*)]#i", '', $text); }
// Pour les adresses email en format texte
if (eregi('@', $text)) { $text = preg_replace("#([ n ])?([ a-z0-9-_.]+)@([ a-z0-9-_.]+)#i", '12(at)3', $text); }
return $text;
}
?>
La première fonction imite les fonctionnalités principales du BBcode alors que la seconde peut vous servir par exemple pour convertir le message en un résumé sans tags pour un fil RSS.
Ce code relativement simple, utilisant tout de même quelques expressions régulières un peu complexes, vérifie la présence du nombre de tags ouvrants et fermants, mais ne réagira pas forcément parfaitement en cas d'inclusions de tags dans d'autres tags, par ailleurs le simple fait de poster ce code dans un forum qui l'utilise peut le déboussoler (cela peut etre corrigé), j'ai donc remplacé tous les "[" des tags par des "[ " ou [+espace pour éviter qu'ils soient interprétés. Vous pouvez évidemment remplacer les "[ " par "[" pour retrouver le code original.
On notera différentes ruses assez impropres mais efficaces pour pouvoir repérer les url et les transformer en liens (ici avec l'attribut rel="nofollow", que vous pouvez enlever) tout en ne les retransformant pas une seconde fois si le tag URL= est également détécté autour de l'url. Il y a bien évidemment des centaines de façons de réaliser cela.
Certaines lignes de code sont là uniquement à titre d'exemple, vous pouvez rajouter autant de smileys ou de couleurs que vous le souhaitez, je n'ai pas la prétention de fournir ici un code parfait, loin de là . Il s'agit là uniquement de vous donner une piste pour construire et adapter le votre à votre forum.
Pour la construction d'un forum en PHP, voir la discussion suivante
Voir aussi ficgs
Plus de sources Vous devez vous enregistrer pour consulter ces sources, vous pourrez alors changer leur ordre en cliquant sur les icones les précédant.
admin
Autres sources Ce projet est collaboratif, vous pouvez remonter les sources suivantes dans la liste si vous les trouvez utiles.
generateur mise en forme text java bbcode Source ☼ PHPCS.com | PHP CodeS SourceS | 3121 sources PHP, Source, Code, PHP Scripts, Programmatio...phpcs > r/code.aspx?r=generateur+mise+en+forme+text+java+bbcode bbCode et RegEx - ASP-PHP.netasp php > ressources/bouts de code.aspx?id=343 2000 partitions gratuites pour tous instruments... SharePoint Personalization Site Links - Les liens personnalisĂ©s des... bbcode_create - CrĂ©e une ressource BBCode - Fonctions BBCode - BBCode - Traitement du texte - RĂ©fĂ©rence des fonctions - Manuel PHP - Le PHP Facilelephpfacile > manuel php/function.bbcode create.php Cette fonction retourne une nouvelle ressource BBCode, utilisĂ©e... FONCTION BBCODE, COMPATIBLE W3C bbcode, fonction, xhtml, Source N°47548 PHPphpcs > codes PHP: bbcode_create - Manualphp > manual/fr/function.bbcode create.php Cette fonction retourne une nouvelle ressource BBCode, utilisĂ©e... For those without the BBCode extension, here's a relatively elegant... BBCode fonction [spoil] - PHP - Programmation - FORUM HardWare.frforum.hardware > hfr/Programmation/PHP verifier plusieurs champs d'un formulaire dans une meme... [PHP][resolu] appel de fonction avec nom gĂ©nĂ©rĂ©... [BBcode] Problème avec la fonction preg_replacesiteduzero > forum 83 430562 p1 bbcode probleme avec la Fonction PHP : bbcode()phpfonctions Utilisation de @font-face pour utiliser n'importe quelle typographie... Tutorial JQuery : trier une liste en drag'n'drop avec JQueryUI... Preg_replace dans fonction : le texte n'apparait plus...siteduzero > forum 83 414381 p1 preg replace dans Probleme fonction affichage bbcodecommentcamarche > forum/affich 11288024 probleme je ne comprends pas ce que tu veux dire, pourrais-tu m'expliquer plus... « En passant au cloud, les entreprises doivent garder le contrĂ´le... Ou positionner la fonction dans un formulaire pour interprĂ©ter le BBCode? - Forum des professionnels en informatiquedeveloppez > forums/d371614/php/langage/regex/positionner Visiblement, il suffit d'utiliser la fonction comme cela :... Et si j'ai bien compris tu veux rajouter des boutons BBCode Ă ... Tutoriel de PHP: BB-CODE - La boĂ®te Ă proglaboiteaprog > article 62 2 php bb code DVDenfrancais.com • Informationsdvdenfrancais > dvd/fr/forum/viewtopic.php?f=28&t=3862 SIMPLE FONCTION TEST LIEN RAPIDSHARE rapidshare, check, cliquable, popup, bbcode, Source N°44028 PHP « Watch the Best Of the Webweelakeo > 2009/07/14/simple // pour rotorne lien cliquable change ($msg) par... Avertissez-moi par courriel lors de la parution de nouveaux... Liste complète des fonctions php & MySQL - Lettre Bphpsources 6 fonctions PHP indispensableslyxia > blog/developpement/php/5 Pour ne rater aucun article, abonnez-vous au flux... Problème PHP : Fonction nl2br()forum.servhome > index.php?topic=4074.0 et par ailleurs, certaines autres utilisations simples de nl2br ne...
Trackbacks : If you talked about this article in your blog or website, you may instantly get a backlink
There's no trackback at the moment.