BBCode2HTML modifier plugin
| From: | Andr� Rabold | Date: | Wed, 13 Nov 2002 13:38:36 +0000 |
| Subject: | BBCode2HTML modifier plugin | ||
| Groups: | php.smarty.general | ||
| Request: | Send a blank email to php-smarty-general+get-6414@lists.php.net to get a copy of this message | ||
/*
* Smarty plugin
* ------------------------------------------------------------
* Type: modifier
* Name: bbcode2html
* Purpose: Converts BBCode style tags to HTML
* Author: André Rabold
* Version: 1.2
* What's new: - now supports CSS classes:
* ng_email (mailto links)
* ng_url (www links)
* ng_quote (quotes)
* ng_quote_body (quotes)
* ng_code (source code)
* ng_list (html lists)
* ng_list_item (list items)
* - replaced slow ereg_replace() functions
* - Alterned [quote] and [code] to use CSS classes
* instead of HTML <blockquote />, <hr />, ... tags.
* - Additional BBCode tags [list] and [*] to display
* nice HTML lists. Example:
* [list]
* [*]first item
* [*]second item
* [*]third item
* [/list]
* The [list] tag can have an additional parameter:
* [list] unorderer list with bullets
* [list=1] ordered list 1,2,3,4,...
* [list=i] ordered list i,ii,iii,iv,...
* [list=I] ordered list I,II,III,IV,...
* [list=a] ordered list a,b,c,d,...
* [list=A] ordered list A,B,C,D,...
* - produces well-formed output
* - cleaned up the code
* ------------------------------------------------------------
*/
function smarty_modifier_bbcode2html($message)
{
$strrep = array(
"[b]" => "<b>",
"[/b]" => "</b>",
"[i]" => "<i>",
"[/i]" => "</i>",
"[u]" => "<u>",
"[/u]" => "</u>",
"[center]" => "<center>",
"[/center]" => "</center>",
"[code]" => "<div class=\"ng_code\">",
"[/code]" => "</div>",
);
$preg = array(
// Font and text manipulation ( [color] [size] [font] [align] )
'/\[color=(.*?)\](.*?)\[\/color\]/si' => "<span
style=\"color:\\1\">\\2</span>",
'/\[size=(.*?)\](.*?)\[\/size\]/si' => "<span
style=\"font-size:\\1\">\\2</span>",
'/\[font=(.*?)\](.*?)\[\/font\]/si' => "<span
style=\"font-family:\\1\">\\2</span>",
'/\[align=(.*?)\](.*?)\[\/align\]/si' => "<div
style=\"text-align:\\1\">\\2</div>",
// [email]
'/\[email\](.*?)\[\/email\]/si' => "<a href=\"mailto:\\1\"
class=\"ng_email\">\\1</a>",
'/\[email=(.*?)\](.*?)\[\/email\]/si' => "<a href=\"mailto:\\1\"
class=\"ng_email\">\\2</a>",
// [url]
'/\[url\]www\.(.*?)\[\/url\]/si' => "<a
href=\"http://www.\\1\" target=\"_blank\"
class=\"ng_url\">\\1</a>",
'/\[url\](.*?)\[\/url\]/si' => "<a href=\"\\1\"
target=\"_blank\" class=\"ng_url\">\\1</a>",
'/\[url=(.*?)\](.*?)\[\/url\]/si' => "<a href=\"\\1\"
target=\"_blank\" class=\"ng_url\">\\2</a>",
// [img]
'/\[img](.*?)\[\/img\]/si' => "<img src=\"\\1\"
border=\"0\" />",
'/\[img=(.*?)x(.*?)\](.*?)\[\/img\]/si' => "<img
width=\"\\1\"
height=\"\\2\" src=\"\\3\" border=\"0\" />",
// [quote]
'/\[quote\](.*?)\[\/quote\]/si' => "<div
class=\"ng_quote\">Quote:<div
class=\"ng_quote_body\">\\1</div></div>",
'/\[quote=(.*?)\](.*?)\[\/quote\]/si' => "<div
class=\"ng_quote\">Quote \\1:<div
class=\"ng_quote_body\">\\2</div></div>",
// [list]
'/\[\*\](.*?)/si' => "<li
class=\"ng_list_item\">\\1</li>",
'/\[list\](.*?)\[\/list\]/si' => "<ul
class=\"ng_list\">\\1</ul>",
'/\[list=1\](.*?)\[\/list\]/si' => "<ol
class=\"ng_list\"
style=\"list-style-type:decimal;\">\\1</ol>",
'/\[list=i\](.*?)\[\/list\]/s' => "<ol
class=\"ng_list\"
style=\"list-style-type:lower-roman;\">\\1</ol>",
'/\[list=I\](.*?)\[\/list\]/s' => "<ol
class=\"ng_list\"
style=\"list-style-type:upper-roman;\">\\1</ol>",
'/\[list=a\](.*?)\[\/list\]/s' => "<ol
class=\"ng_list\"
style=\"list-style-type:lower-alpha;\">\\1</ol>",
'/\[list=A\](.*?)\[\/list\]/s' => "<ol
class=\"ng_list\"
style=\"list-style-type:upper-alpha;\">\\1</ol>",
);
$message = str_replace(array_keys($strrep),array_values($strrep),
$string);
$message = preg_replace(array_keys($preg),array_values($preg),
$string);
return $message;
}