preg_replace would be better for this.
Here is a function I use:
PHP Code:
function bbcode_format($str) {
$str = htmlentities($str);
$simple_search = array(
'/\[h1\](.*?)\[\/h1\]/is',
'/\[h2\](.*?)\[\/h2\]/is',
'/\[h3\](.*?)\[\/h3\]/is',
'/\[h4\](.*?)\[\/h4\]/is',
'/\[b\](.*?)\[\/b\]/is',
'/\[i\](.*?)\[\/i\]/is',
'/\[u\](.*?)\[\/u\]/is',
'/\[s\](.*?)\[\/s\]/is',
'/\[LINK=(.*?)\](.*?)\[\/LINK]/is',
'/\[red\](.*?)\[\/red]/is',
'/\[blue\](.*?)\[\/blue]/is',
'/\[IMG\](.*?)\[\/IMG\]/is',
);
$simple_replace = array(
'<h1>$1</h1>',
'<h2>$1</h2>',
'<h3>$1</h3>',
'<h4>$1</h4>',
'<strong>$1</strong>',
'<em>$1</em>',
'<u>$1</u>',
'<s>$1</s>',
'<a href="$1">$2</a>',
'<span style="color: red">$1</span>',
'<span style="color: blue">$1</span>',
'<div class="img_border"><img src="'. constant('IMAGE_PATH') .'/thumbs/$1" alt="$1"/></div>',
);
// Do simple BBCode's
$str = preg_replace ($simple_search, $simple_replace, $str);
return $str;
}
usage
PHP Code:
$txt = "this is [link="http://www.sitepoint.com"]Go To Sitepoint[/link]
echo bbcode_format($txt);
Try it and see what happens
Bookmarks