Asi nejako takto:
function match_bbcode ($matches)
{
$bbcodes = array
(
"b" => '<b>\1</b>',
"i" => '<i>\1</i>',
"u" => '<u>\1</u>',
"img" => '<img src="\1" />',
"url" => '<a href="\1">\1</a>'
);
return (isset ($bbcodes [$matches [1]])?
str_replace ('\1', $matches [2], $bbcodes [$matches [1]]):
$matches [0]);
}
$html_text = nl2br (preg_replace_callback ('/\[(\w+)\]([\s\S]*?)\[\/\1\]/', "match_bbcode", htmlspecialchars ($bb_text)));