Simple BBCode To HTML Function In PHP
While working on a "secret" project I needed a simple BBCode to HTML converter. I'm sure there are a number of them out there, but I wanted to build my own. Here it is, a quick and dirty regex filled function.
function bbc2html($content) {
$content = preg_replace('/(\[b\])(.*?)(\[\/b\])/','$2',$content);
$content = preg_replace('/(\[i\])(.*?)(\[\/i\])/','$2',$content);
$content = preg_replace('/(\[u\])(.*?)(\[\/u\])/','$2',$content);
$content = preg_replace('/(\[ul\])(.*?)(\[\/ul\])/','
- $2
In other news this is post #99 since I started the blog. Here's to hoping #100 is stellar.
Update (01/23/07)
I just realized that PHP lets you use arrays in preg_replace, so a 'better' version would be...$2 ',
'$4',
'$2'
);
return preg_replace($search, $replace, $content);
}
I just realized that PHP lets you use arrays in preg_replace, so a 'better' version would be...
function bbc2html($content) {
$search = array (
'/(\[b\])(.*?)(\[\/b\])/',
'/(\[i\])(.*?)(\[\/i\])/',
'/(\[u\])(.*?)(\[\/u\])/',
'/(\[ul\])(.*?)(\[\/ul\])/',
'/(\[li\])(.*?)(\[\/li\])/',
'/(\[url=)(.*?)(\])(.*?)(\[\/url\])/',
'/(\[url\])(.*?)(\[\/url\])/'
);
$replace = array (
'$2',
'$2',
'$2',
'
- $2