Simple PHP BBCode

, ,

Many websites allow visitors to contribute to the sites’s content through forums, blogs etc . When you’re building such sites, it’s a good idea to provide users with the ability to enhance that content by posting formatted text. Here is a simple example of how to use bbcode with php.

$text="[b]some text[b] using bbcode";

$find = array(
"'\[b\](.*?)\[/b\]'i",
"'\[i\](.*?)\[/i\]'i",
"'\[u\](.*?)\[/u\]'i",
"'\[link\](.*?)\[/link\]'i",
"'\[link=(.*?)\](.*?)\[/link\]'i",
"'\[quote=(.*?)\](.*?)\[/quote\]'",
"'\[quote\](.*?)\[/quote\]'",
);
$replace = array(
"<strong>\\1</strong>",
"<em>\\1</em>",
"<span style=\"text-decoration: underline;\">\\1</span>",
"<a href=\"\\1\">\\1</a>",
"<a href=\"\\1\">\\2</a>",
"<blockquote><div style=\"font-size: 12px;\">quote (\\1):</div><div style=\"border:1px solid #ACA899;\">\\2</div></blockquote>",
"<blockquote><div style=\"font-size: 12px;\">quote:</div><div style=\"border:1px solid #ACA899;\">\\1</div></blockquote>",
);

$text=preg_replace($find,$replace,$text);


Comments are closed.