TRANSFORMER DU PBB CODE EN CODE HTML

jjDai Messages postés 291 Date d'inscription vendredi 21 février 2003 Statut Membre Dernière intervention 13 mars 2015 - 23 avril 2011 à 10:57
Reldan Messages postés 14 Date d'inscription mercredi 27 octobre 2010 Statut Membre Dernière intervention 27 mai 2013 - 26 avril 2011 à 16:31
Cette discussion concerne un article du site. Pour la consulter dans son contexte d'origine, cliquez sur le lien ci-dessous.

https://codes-sources.commentcamarche.net/source/53083-transformer-du-pbb-code-en-code-html

Reldan Messages postés 14 Date d'inscription mercredi 27 octobre 2010 Statut Membre Dernière intervention 27 mai 2013
26 avril 2011 à 16:31
correction :

public static function bbcode2html($text){
$text = preg_replace('!\[quote\](.+)\/quote\!isU', '
Citation :
$1
', $text);
$text = preg_replace("!\[quote\=(.+)\](.+)\[\/quote\]!isU", "
$1 :
$2
", $text);
$text = preg_replace('!\[b\](.+)\/b\!isU', '$1', $text);
$text = preg_replace('!\[i\](.+)\/i\!isU', '$1', $text);
$text = preg_replace('!\[u\](.+)\/u\!isU', '$1', $text);
//$text = preg_replace('!\[center\](.+)\/center\!isU', '$1

', $text);
//$text = preg_replace('!\[right\](.+)\/right\!isU', '$1

', $text);
//$text = preg_replace('!\[left\](.+)\/left\!isU', '$1

', $text);
//$text = preg_replace('!\[titre\](.+)\/titre\!isU', '$1
',$text);
//$text = preg_replace('!\[email\](.+)\/email\!isU', '["mailto:$1">$1',$text);
//$text = preg_replace('!\[img\](.+)\/img\!isU', '',$text);
$text = preg_replace('!\[url\](.+)\/url\!isU', '$1',$text);
$text = preg_replace("!\[url=([^\]]+)\](.+)\/url\!Ui","$2",$text);
$text = preg_replace("!\[color=([^\]]+)\](.+)\[\/color\]!mi","$2",$text);
$text = preg_replace("!\[list\](.+)\/list\!isU","
$1
",$text);
//$text = preg_replace("!\[list=([^\]]+)\](.+)\/list\!isU","
$2
",$text);
$text = preg_replace("!\[\*\](.+)(?=(\[\*\]|
))!isU"," * $1
",$text);
$text = preg_replace('!\[code\](.+)\/code\!isU', '$1

', $text);
return(nl2br($text));
}
Reldan Messages postés 14 Date d'inscription mercredi 27 octobre 2010 Statut Membre Dernière intervention 27 mai 2013
26 avril 2011 à 16:26
Voilà la méthode que j'utilise (pour info je crois qu'on appel ça du bbCode et non du pbb)
j'ai mis en commentaires les tags que je n'utilise pas sur mon site (parce que ça doit être assez gourmand l'accumulation de preg_replace)

class StringHelper{

[...]

public static function bbcode2html($text){
$text = preg_replace('!\[quote\](.+)\/quote\!isU', '
Citation :
$1
', $text);
$text = preg_replace("!\[quote\=(.+)\](.+)\[\/quote\]!isU", "
$1 :
$2
", $text);
$text = preg_replace('!\[b\](.+)\/b\!isU', '$1', $text);
$text = preg_replace('!\[i\](.+)\/i\!isU', '$1', $text);
$text = preg_replace('!\[u\](.+)\/u\!isU', '$1', $text);
//$text = preg_replace('!\[center\](.+)\/center\!isU', '$1

', $text);
//$text = preg_replace('!\[right\](.+)\/right\!isU', '$1

', $text);
//$text = preg_replace('!\[left\](.+)\/left\!isU', '$1

', $text);
//$text = preg_replace('!\[titre\](.+)\/titre\!isU', '$1
',$text);
//$text = preg_replace('!\[email\](.+)\/email\!isU', '["mailto:$1">$1',$text);
//$text = preg_replace('!\[img\](.+)\/img\!isU', '',$text);
$text = preg_replace('!\[url\](.+)\/url\!isU', '$1',$text);
$text = preg_replace("!\[url=([^\]]+)\](.+)\/url\!Ui","",$text);
//$text = str_replace ( '/size' , '' , $text );
$text = preg_replace("!\[color=([^\]]+)\](.+)\[\/color\]!mi","$2",$text);
//$text = str_replace ( '/color' , '</color>' , $text );
$text = preg_replace("!\[list\](.+)\/list\!isU","
$1
",$text);
//$text = preg_replace("!\[list=([^\]]+)\](.+)\/list\!isU","
$2
",$text);
$text = preg_replace("!\[\*\](.+)(?=(\[\*\]|
))!isU"," * $1
",$text);
//$text = str_replace('
', '', $text);
//$text = str_replace('
', '

', $text);
return(nl2br($text));
}
}
jjDai Messages postés 291 Date d'inscription vendredi 21 février 2003 Statut Membre Dernière intervention 13 mars 2015
25 avril 2011 à 00:02
Merci pour l'explication
JJDAI
cs_audain Messages postés 9 Date d'inscription vendredi 31 octobre 2008 Statut Membre Dernière intervention 28 avril 2011
24 avril 2011 à 13:48
je vais voire je promet rien.
Morpion91 Messages postés 1 Date d'inscription vendredi 7 mars 2003 Statut Membre Dernière intervention 24 avril 2011
24 avril 2011 à 08:10
Salut. Simple et pas mal pour moi.
Serait-il possible de compléter ta liste ?
Pour ca, va voir sur 'http://fr.wikipedia.org/wiki/BBCode'
Merci et bon courage
cod57 Messages postés 1653 Date d'inscription dimanche 7 septembre 2008 Statut Membre Dernière intervention 11 septembre 2013 19
23 avril 2011 à 23:23
bonsoir
@JJDAI
ça sert surtout dans les blogs et les livres d'or pour faire du code 'like' html et raccourçi
On place dans un textarea son bbcode PUIS php traduit aprés en lecture
En meme temps tu evites par exemple de stocker du html dans tes messages voir du javascript (tentative de hack ... XSS)

http://www.php.net/manual/fr/book.bbcode.php

exemple

echo encode_pbb('test');
echo '
';
echo encode_pbb('test');
echo '
';
echo encode_pbb('');

si tu fais 'voir le code source' dans ton navigateur tu veras c'est traduit en html

a++
jjDai Messages postés 291 Date d'inscription vendredi 21 février 2003 Statut Membre Dernière intervention 13 mars 2015
23 avril 2011 à 10:57
Bonjour
Execusez mon ignorance, mais C'est quoi le PBB ?
JJDAI
Rejoignez-nous