...

View Full Version : BBcode with Regular Expressions



polearm
12-01-2005, 03: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, 06: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, 06:28 PM
The dot should also match newlines Youd have to add an "s" to the "i".

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

marek_mar
12-28-2005, 07: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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum