*/
class BBcode
{
/**
* Holds the bbcode parsers
*
* @var array
*/
public static $parsers = [
'bold' => [
'pattern' => '/\[b\](.*?)\[\/b\]/s',
'replace' => '$1',
'content' => '$1',
],
'italic' => [
'pattern' => '/\[i\](.*?)\[\/i\]/s',
'replace' => '$1',
'content' => '$1',
],
'underline' => [
'pattern' => '/\[u\](.*?)\[\/u\]/s',
'replace' => '$1',
'content' => '$1',
],
'linethrough' => [
'pattern' => '/\[s\](.*?)\[\/s\]/s',
'replace' => '$1',
'content' => '$1',
],
'header' => [
'pattern' => '/\[header\](.*?)\[\/header\]/s',
'replace' => '
$1
',
'content' => '$1',
],
'size' => [
'pattern' => '/\[size\=([1-7])\](.*?)\[\/size\]/s',
'replace' => '$2',
'content' => '$2',
],
'color' => [
'pattern' => '/\[color\=(#[A-f0-9]{6}|#[A-f0-9]{3})\](.*?)\[\/color\]/s',
'replace' => '$2',
'content' => '$2',
],
'center' => [
'pattern' => '/\[center\](.*?)\[\/center\]/s',
'replace' => '$1
',
'content' => '$1',
],
'left' => [
'pattern' => '/\[left\](.*?)\[\/left\]/s',
'replace' => '$1
',
'content' => '$1',
],
'right' => [
'pattern' => '/\[right\](.*?)\[\/right\]/s',
'replace' => '$1
',
'content' => '$1',
],
'align' => [
'pattern' => '/\[align\=(left|center|right)\](.*?)\[\/align\]/s',
'replace' => '$2
',
'content' => '$2',
],
'quote' => [
'pattern' => '/\[quote\](.*?)\[\/quote\]/s',
'replace' => '$1
',
'content' => '$1',
],
'namedquote' => [
'pattern' => '/\[quote\=(.*?)\](.*)\[\/quote\]/s',
'replace' => '$1$2
',
'content' => '$2',
],
'link' => [
'pattern' => '/\[url\](.*?)\[\/url\]/s',
'replace' => '$1',
'content' => '$1',
],
'namedlink' => [
'pattern' => '/\[url\=(.*?)\](.*?)\[\/url\]/s',
'replace' => '$2',
'content' => '$2',
],
'image' => [
'pattern' => '/\[img\](.*?)\[\/img\]/s',
'replace' => '
',
'content' => '$1',
],
'orderedlistnumerical' => [
'pattern' => '/\[list=1\](.*?)\[\/list\]/s',
'replace' => '$1
',
'content' => '$1',
],
'orderedlistalpha' => [
'pattern' => '/\[list=a\](.*?)\[\/list\]/s',
'replace' => '$1
',
'content' => '$1',
],
'unorderedlist' => [
'pattern' => '/\[list\](.*?)\[\/list\]/s',
'replace' => '',
'content' => '$1',
],
'listitem' => [
'pattern' => '/\[\*\](.*)/',
'replace' => '$1',
'content' => '$1',
],
'code' => [
'pattern' => '/\[code\](.*?)\[\/code\]/s',
'replace' => '$1
',
'content' => '$1',
],
'youtube' => [
'pattern' => '/\[youtube\](.*?)\[\/youtube\]/s',
'replace' => '',
'content' => '$1',
],
'linebreak' => [
'pattern' => '/\r\n|\r|\n/',
'replace' => '
',
'content' => '',
],
];
/**
* Parse the emoticons.
*
* @param string $text
* @return string
*/
public static function parseEmoticons($text)
{
// Get emoticons from the database
$emotes = DB::table('emoticons')
->get();
// Parse all emoticons
foreach ($emotes as $emote) {
$image = "
";
$icon = preg_quote($emote->emote_string, '#');
$text = preg_replace("#{$icon}#", $image, $text);
}
// Return the parsed text
return $text;
}
/**
* Convert the parsed text to HTML.
*
* @param string $text
* @return string
*/
public static function toHTML($text)
{
$text = self::parseEmoticons($text);
foreach (self::$parsers as $parser) {
$text = preg_replace($parser['pattern'], $parser['replace'], $text);
}
return $text;
}
}