Page 1 of 1

[RESOLVED] bbcode getting on my nerves now -.-

Posted: Mon Feb 21, 2011 10:08 pm
by Torniquet
ok, currently trying to script a function to parse bbcode, and its bugging me why its not working!

ill start with the DB entries as they stand.. (well the 2 that are inconsistant)

1st

Code: Select all

[quote][b]Viral[/b]
Ads[/quote]
[i]frostttttt[/i]
2nd

Code: Select all

[quote]a poll[u]lllll[/u][i]l[/i]llllll[/quote]
here is the function...

Code: Select all

function bbcode_format($str){
	$str = htmlentities(html_entity_decode($str));
	
	$bbcode = array(
			'/\[quote\](.*?)\[\/quote\]/' => '<h1>$1</h1>',
			'/\[b\](.*?)\[\/b\]/' => '<strong>$1</strong>',
			'/\[u\](.*?)\[\/u\]/' => '<u>$1</u>',
			'/\[i\](.*?)\[\/i\]/' => '<em>$1</em>',
			);
	
	foreach($bbcode as $key => $value){
		$str = preg_replace($key, $value, $str);
	}
	return $str;
}

see attached image for the output.

i cant get my head round why it is changing one quote code, but not the other. every other one seems to work fine, but the 1st output one.

can anyone see something i cant? or have a reasonable explination as to why the heck its not working -.-


EDIT ~~~

ok just noticed its because of the line breaks. if i remove them then they it is dandy... so why wont it work with a break...

i will leave this post unresolved until either i work out or a suggestion is posted :)

EDIT 2 ~~~

ok seems sorted now, once i knew what i was looking for i found the solution. it needed a trailing s modifier on the end of the keys.

code if anyone wants it.

Code: Select all

function bbcode_format($str){
	$str = htmlentities(html_entity_decode($str));
	
	$bbcode = array(
			'/\[quote\](.*?)\[\/quote\]/s' => '<h1>$1</h1>',
			'/\[b\](.*?)\[\/b\]/s' => '<strong>$1</strong>',
			'/\[u\](.*?)\[\/u\]/s' => '<u>$1</u>',
			'/\[i\](.*?)\[\/i\]/s' => '<em>$1</em>',
			);
	
	foreach($bbcode as $key => $value){
		$str = preg_replace($key, $value, $str);
	}
	return nl2br($str);
}