Helpful Information
 
 
Category: Post a PHP snippet
BBcode with Regular Expressions

<?php
function bbcode_html($input) {
$bbcode = array(
'/\[b\](.+?)\[\/b\]/i',
'/\[i\](.+?)\[\/i\]/i',
'/\[quote\](.+?)\[\/quote\]/i',
'/\[url=(.+?)\](.+?)\[\/url\]/i'
);

$html = array(
'<strong>$1</strong>',
'<em>$1</em>',
'<blockquote>$1</blockquote>',
'<a href="$1">$2</a>'
);

return preg_replace($bbcode, $html, $input);
}
?>

Although this code works when you write it on one line, it wont work if you do the following...

[ quote ]
hello
world
[ /quote ]

because of the multiple lines.

Does anyone know how to fix this?

The dot should also match newlines Youd have to add an "s" to the "i".

What if i wanted to nest quotes (which is usually standard in bbcode)?

It should work as long as you don't nest the same bbcode (eg.

...Why not?) which will fail but getting that right is kind of difficult.










privacy (GDPR)