<?php
class BBCode {
protected $bbcode_table = array();
public function __construct () {
$this->bbcode_table["/\[b\](.*?)\[\/b\]/is"] = function ($match) {
return "<strong>$match[1]</strong>";
};
$this->bbcode_table["/\[i\](.*?)\[\/i\]/is"] = function ($match) {
return "<em>$match[1]</em>";
};
$this->bbcode_table["/\[code\](.*?)\[\/code\]/is"] = function ($match) {
return "<pre><code>$match[1]</code></pre>";
};
$this->bbcode_table["/\[quote\](.*?)\[\/quote\]/is"] = function ($match) {
return "<blockquote><p>$match[1]</p></blockquote>";
};
$this->bbcode_table["/\[quote=\"([^\"]+)\"\](.*?)\[\/quote\]/is"] = function ($match) {
return "$match[1] wrote: <blockquote><p>$match[2]</p></blockquote>";
};
$this->bbcode_table["/\[size=(\d+)\](.*?)\[\/size\]/is"] = function ($match) {
return "<span style=\"font-size:$match[1]%\">$match[2]</span>";
};
$this->bbcode_table["/\[s\](.*?)\[\/s\]/is"] = function ($match) {
return "<del>$match[1]</del>";
};
$this->bbcode_table["/\[u\](.*?)\[\/u\]/is"] = function ($match) {
return '<span style="text-decoration:underline;">' . $match[1] . '</span>';
};
$this->bbcode_table["/\[center\](.*?)\[\/center\]/is"] = function ($match) {
return '<div style="text-align:center;">' . $match[1] . '</div>';
};
$this->bbcode_table["/\[right\](.*?)\[\/right\]/is"] = function ($match) {
return '<div style="text-align:right;">' . $match[1] . '</div>';
};
$this->bbcode_table["/\[left\](.*?)\[\/left\]/is"] = function ($match) {
return '<div style="text-align:left;">' . $match[1] . '</div>';
};
$this->bbcode_table["/\[justify\](.*?)\[\/justify\]/is"] = function ($match) {
return '<div style="text-align:justify;">' . $match[1] . '</div>';
};
$this->bbcode_table["/\[color=([#a-z0-9]+)\](.*?)\[\/color\]/is"] = function ($match) {
return '<span style="color:'. $match[1] . ';">' . $match[2] . '</span>';
};
$this->bbcode_table["/\[email\](.*?)\[\/email\]/is"] = function ($match) {
return "<a href=\"mailto:$match[1]\">$match[1]</a>";
};
$this->bbcode_table["/\[email=(.*?)\](.*?)\[\/email\]/is"] = function ($match) {
return "<a href=\"mailto:$match[1]\">$match[2]</a>";
};
$this->bbcode_table["/\[url\](.*?)\[\/url\]/is"] = function ($match) {
return "<a href=\"$match[1]\">$match[1]</a>";
};
$this->bbcode_table["/\[url_blank\](.*?)\[\/url_blank\]/is"] = function ($match) {
return "<a href=\"$match[1]\" target=\"_blank\">$match[1]</a>";
};
$this->bbcode_table["/\[url=(.*?)\](.*?)\[\/url\]/is"] = function ($match) {
return "<a href=\"$match[1]\">$match[2]</a>";
};
$this->bbcode_table["/\[img\](.*?)\[\/img\]/is"] = function ($match) {
return "<img src=\"../$match[1]\"/>";
};
$this->bbcode_table["/\[list\](.*?)\[\/list\]/is"] = function ($match) {
$match[1] = preg_replace_callback("/\[\*\]([^\[\*\]]*)/is", function ($submatch) {
return "<li>" . preg_replace("/[\n\r?]$/", "", $submatch[1]) . "</li>";
}, $match[1]);
return "<ul>" . preg_replace("/[\n\r?]/", "", $match[1]) . "</ul>";
};
$this->bbcode_table["/\[list=(1|a)\](.*?)\[\/list\]/is"] = function ($match) {
if ($match[1] == '1') {
$list_type = '<ol>';
} else if ($match[1] == 'a') {
$list_type = '<ol style="list-style-type: lower-alpha">';
} else {
$list_type = '<ol>';
}
$match[2] = preg_replace_callback("/\[\*\]([^\[\*\]]*)/is", function ($submatch) {
return "<li>" . preg_replace("/[\n\r?]$/", "", $submatch[1]) . "</li>";
}, $match[2]);
return $list_type . preg_replace("/[\n\r?]/", "", $match[2]) . "</ol>";
};
$this->bbcode_table["/\[youtube\](?:http?:\/\/)?(?:www\.)?youtu(?:\.be\/|be\.com\/watch\?v=)([A-Z0-9\-_]+)(?:&(.*?))?\[\/youtube\]/i"] = function ($match) {
return "<iframe class=\"youtube-player\" type=\"text/html\" width=\"340\" height=\"285\" src=\"http://www.youtube.com/embed/$match[1]\" frameborder=\"0\"></iframe>";
};
}
public function replace ($str, $escapeHTML=false, $nr2br=false) {
foreach($this->bbcode_table as $key => $val) {
$str = preg_replace_callback($key, $val, $str);
}
return nl2br($str);
}
function bbcode_more($stringa, $id)
{
if(strpos($stringa,"[/MORE]")){
$str=explode("[/MORE]",$stringa);
if(trim($str[0]) !="[MORE]"){
$dopo = trim(str_replace($str[0], "<div class='post_back_button'><p><a href='news_view.php?id=$id' >Leggi Tutto</a></p></div>", $str[0]));
$prima=str_replace("[MORE]","",$str[0]);
return $prima."... ".$dopo;
}else{
$stringa=str_replace("[MORE]","",$stringa);
$stringa=str_replace("[/MORE]","",$stringa);
return $stringa;
}
}else{
return $stringa;
}
}
function bbcode_complete($string)
{
$string = $this->replace($string);
$string = trim(str_replace("[MORE]", "", $string));
$string = trim(str_replace("[/MORE]", "", $string));
return $string;
}
}
?>