En diversos foros es común ver a usuarios preguntando cómo implementar BBCode en sus proyectos. Por esta razón, a continuación se presenta una clase de ejemplo que permite integrar esta útil herramienta y añadir funcionalidad adicional a tus aplicaciones.
La clase es bastante sencilla y fácil de implementar. Además, puedes ampliarla y adaptarla según tus propias necesidades.
class BBCode {
private $_bbcodes = array(
'type1' => array(
'spoiler' => array(
'html' => '<div class="spoiler_container"><div class="spoiler_bar"><input class="i_button" type="button" value="spoiler +" onclick="toogleSpoiler(this);"></div><div class="spoiler_content">$1</div></div>',
'pattern' => '#\[spoiler](.*?)\[\/spoiler]#is'),
'cite' => array(
'html' => '<div class="cite"><div class="bgclose">$1</div></div>',
'pattern' => '#\[cite](.*?)\[\/cite]#is'),
'shell' => array(
'html' => '<div class="shell">$1</div>',
'pattern' => '#\[shell](.*?)\[\/shell]#is'),
'box' => array(
'html' => '<div class="box">$1</div>',
'pattern' => '#\[box](.*?)\[\/box]#is'),
'text' => array(
'html' => '<div class="text">$1</div>',
'pattern' => '#\[text](.*?)\[\/text]#is'),
'bold' => array(
'html' => '<b>$1</b>',
'pattern' => '#\[b](.*?)\[\/b]#is'),
'italic' => array(
'html' => '<i>$1</i>',
'pattern' => '#\[i](.*?)\[\/i]#is'),
'underline' => array(
'html' => '<u>$1</u>',
'pattern' => '#\[u](.*?)\[\/u]#is'),
'strike' => array(
'html' => '<strike>$1</strike>',
'pattern' => '#\[strike](.*?)\[\/strike]#is'),
'left' => array(
'html' => '<div style="text-align:left;">$1</div>',
'pattern' => '#\[left](.*?)\[\/left]#is'),
'center' => array(
'html' => '<div style="text-align:center;">$1</div>',
'pattern' => '#\[center](.*?)\[\/center]#is'),
'right' => array(
'html' => '<div style="text-align:right;">$1</div>',
'pattern' => '#\[right](.*?)\[\/right]#is'),
'img' => array(
'html' => '<img style="max-width: 100%" src="$1" alt="">',
'pattern' => '#\[img](.*?[\.jpg|\.gif|\.png])\[\/img]#is'),
'url' => array(
'html' => '<a href="$1">$1</a>',
'pattern' => '#\[url](.*?)\[\/url]#is'),
'youtube' => array(
'html' => '<iframe width="640" height="360" src="//www.youtube.com/embed/$1?feature=player_detailpage" frameborder="0" allowfullscreen=""></iframe>',
'pattern' => '#\[youtube].*\?v=(.*?)\[\/youtube]#is'),
'vimeo' => array(
'html' => '<iframe src="http://player.vimeo.com/video/$1" width="640" height="360" frameborder="0"></iframe>',
'pattern' => '#\[vimeo](.*?)\[\/vimeo]#is')
),
'type2' => array(
'url' => array(
'html' => '<a href="$1">$2</a>',
'pattern' => '#\[url=(.*?)](.*?)\[\/url]#is'),
'size' => array(
'html' => '<font size="$1">$2</font>',
'pattern' => '#\[size=([1-6])\](.*?)\[\/size]#is'),
'color' => array(
'html' => '<font color="$1">$2</font>',
'pattern' => '#\[color=(\#[0-9a-fA-F]{3,6})\](.*?)\[\/color]#is'),
'quote' => array(
'html' => '<div class="quote">Publicado por: $1<br>$2</div>',
'pattern' => '#\[quote=([0-9]{1,6})\](.*?)\[\/quote]#is'),
'code' => array(
'html' => '<div class="code"><b>code: $1</b><pre class="container_code"><code class="$1">$2</code></pre></div>',
'pattern' => '#\[code=(sql|html|css|js|php)](.*?)\[\/code]#is')
)
);
public function toHtml($content) {
$content = htmlentities($content, ENT_QUOTES, 'UTF-8');
$content = $this->replaceBbcodesType1($content);
$content = $this->replaceBbcodesType2($content);
$content = str_replace("\r", '<br>', $content);
$content = str_replace("<br>", '', $content);
return $content;
}
public function replaceBbcodesType1($content) {
foreach ($this->_bbcodes['type1'] as $bbcode) {
while (preg_match_all($bbcode['pattern'], $content, $matches, PREG_SET_ORDER)) {
foreach ($matches as $match) {
$full = $match[0];
$expr = $match[1];
$newhtml = str_ireplace('$1', $expr, $bbcode['html']);
$content = str_ireplace($full, $newhtml, $content);
}
}
}
return $content;
}
public function replaceBbcodesType2($content) {
foreach ($this->_bbcodes['type2'] as $key => $bbcode) {
while (preg_match_all($bbcode['pattern'], $content, $matches, PREG_SET_ORDER)) {
foreach ($matches as $match) {
$full = $match[0];
$param = $match[1];
$expr = $match[2];
if ($key === 'code') {
$expr = trim($expr);
}
$newhtml = str_ireplace('$1', $param, $bbcode['html']);
$newhtml = str_ireplace('$2', $expr, $newhtml);
$content = str_ireplace($full, $newhtml, $content);
}
}
}
return $content;
}
}
Ejemplo:
$bbcode = new BBCode();
$result = $bbcode->toHtml('[b]Hola mundo[/b]');
echo $result;
Resultado:
Hola mundo