PDA

View Full Version : BB Code?



MrCraig
21-12-2007, 02:39 PM
Hey,

Does anyone know a good BB Code function that parses it no matter if it is on the same line or not.

I currently have


<?php
function bb($str)
{
$str = preg_replace('#\[b\](.*?)\[/b\]#i','<strong>$1</strong>',$str);
$str = preg_replace('#\[i\](.*?)\[/i\]#i','<em>$1</em>',$str);
$str = preg_replace('#\[u\](.*?)\[/u\]#i','<u>$1</u>',$str);
$str = preg_replace('#\[align=(.*?)\](.*?)\[/align\]#i','<div align="$1">$2</div>',$str);
$str = preg_replace('#\[color=(.*?)\](.*?)\[/color\]#i','<font color="$1">$2</font>',$str);
$str = preg_replace('#\[font=(.*?)\](.*?)\[/font\]#i','<font face="$1">$2</font>',$str);
$str = preg_replace('#\[size=(.*?)\](.*?)\[/size\]#i','<font size="$1">$2</font>',$str);
$str = preg_replace('#\[img\](.*?)\[/img\]#i','<img src="$1" />',$str);
$str = preg_replace('#\[code\](.*?)\[/code\]#i','<fieldset><legend>Code</legend><br />$1</fieldset>',$str);
$str = preg_replace('#\[quote\](.*?)\[/quote\]#i','<fieldset><legend>Quote</legend><br />$1</fieldset>',$str);
$str = preg_replace('#\[quote=(.*?)\](.*?)\[/quote\]#i','<fieldset><legend>Quote - $1</legend><br />$2</fieldset>',$str);
$str = preg_replace('#\[url\](.*?)\[/url\]#i','<a href="$1" target="_blank">$1</a>',$str);
$str = preg_replace('#\[url=(.*?)\](.*?)\[/url\]#i','<a href="$1" target="_blank">$2</a>',$str);
return $str;
}
?>


However, it only works if its on the same line.

Thanks for any help =]

Florx
21-12-2007, 02:57 PM
I found this on google



<?php
function bb2html($text)
{
$bbcode = array("<", ">",
"", " ", "",
"", "",
"", "",
"", "",
"", "",
'[color="', "[/color]",
"[size=\"", "[/size]",
'[url="', "[/url]",
"[mail=\"", "[/mail]",
"
", "",
"
", "",
'"]');
$htmlcode = array("&lt;", "&gt;",
"<ul>", "<li>", "</ul>",
"<img src=\"", "\">",
"<b>", "</b>",
"<u>", "</u>",
"<i>", "</i>",
"<span style=\"color:", "</span>",
"<span style=\"font-size:", "</span>",
'<a href="', "</a>",
"<a href=\"mailto:", "</a>",
"<code>", "</code>",
"<table width=100% bgcolor=lightgray><tr><td bgcolor=white>", "</td></tr></table>",
'">');
$newtext = str_replace($bbcode, $htmlcode, $text);
$newtext = nl2br($newtext);//second pass
return $newtext;
}
?>

MrCraig
21-12-2007, 03:25 PM
mm. Might try that

Looks like it might end up in unclosed tags though :S?

MrCraig
22-12-2007, 08:50 PM
Um i take it nobodys got any more ideas?

-Note to MODs: Bumping my old thread uses less resources than posting new one :)

Blob
23-12-2007, 11:15 AM
I have one (a whole class) but I dunno if Im allowed to give it out seems as its from a secret version of kp. ill see what he says.

MrCraig
23-12-2007, 12:33 PM
Thanks :)

Invent
23-12-2007, 12:40 PM
I have one (a whole class) but I dunno if Im allowed to give it out seems as its from a secret version of kp. ill see what he says.

If it's Dan's BBCode parser, he's already posted it on a forum.

Blob
23-12-2007, 12:42 PM
If it's Dan's BBCode parser, he's already posted it on a forum.

Its not its a brand new one...

Edit: Paste it here, to see if it is.

Invent
23-12-2007, 12:44 PM
I don't have it, he just posted it on SOM.

Blob
23-12-2007, 12:46 PM
SOM? Well, I think its new anyways. How recent was the post?

Want to hide these adverts? Register an account for free!