PDA

View Full Version : BBcode with Regular Expressions



polearm
12-01-2005, 02:13 AM
<?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);
}
?>

zealotgi
12-28-2005, 05:14 PM
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?

marek_mar
12-28-2005, 05:28 PM
The dot should also match newlines Youd have to add an "s" to the "i".

zealotgi
12-28-2005, 05:30 PM
What if i wanted to nest quotes (which is usually standard in bbcode)?

marek_mar
12-28-2005, 06:00 PM
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.