views:

296

answers:

6

I would like a function that parses BB Code from vBulletin into a standard HTML markup.

Without using the PEAR library or the PECL extension, because I don't want to fuss with PEAR or have to depend on being able to install a PECL extension on every instance of this application. The goal is zero dependencies. It would be fine if I could find the source code for the PEAR extension and modify that, but I seem to be unable to.

Specifically the trouble I'm having is matching [quote=My Name]. The name 'My Name' isn't enclosed by anything and can contain spaces.

+1  A: 

Well, in addition to the PEAR package and the PECL extension you also have a Zend Framework Package called Zend_Markup which is very easy to use (ZF is loosely coupled, so you can choose to only use that component). There are also quite a few classes over at PHP Classes.

mqchen
+1  A: 

One way to do it would be to use http://www.christian-seiler.de/projekte/php/bbcode/index_en.html

The project page also contains links to similar efforts; one of them might be useful even if you don't like that one.

Voyagerfan5761
A: 

I suggest you just adapt the PEAR extension. It has no dependencies on other PEAR libraries, so it should be fairly straightforward.

Artefacto
Do you have a link to the source code for the PEAR extension? I have been unable to find it.
Josh K
@Josh K See http://pear.php.net/package/HTML_BBCodeParser/download or http://pear.php.net/package/Text_Wiki/download or
Artefacto
A: 

One of the hardest ways is using regex:

$text = "[quote=my name]something.
[b]bla[b]
blabla.[/quote]";
$search = "/\[quote=(?>([a-z0-9]*))\](.*)\[/quote\]/is";
$replace = "From <i>$1</i>: <q>$2</q>";
$return_text = preg_replace($search, $replace, $text);

echo nl2br($return_text);

/*
From <i>my name</i>: <q>something.
[b]bla[b]
blabla.</q>
*/

I say hardest only because of the chance of catastrophic backtracking on that '(.*)', especially with longer text, and a chance of some matches slipping through the cracks. So, you may want to go straight to the source to find the proper regex: http://www.bbcode.org/implementations.php (see: Simple and Complex BBCode with PHP for regex, phpBBCode for sourcecode). Alternatively, you can duplicate and build upon Pear's parser sourcecode here: http://svn.php.net/viewvc/pear/packages/HTML_BBCodeParser

bob-the-destroyer
A: 

Check out my post on creating a bb Code parser function in PHP 5. It is compatible with PHP 5.3.

http://www.redbonzai.com/blog/?p=184

Christian

Christian
A: 

(intro: 5 years developing with vBulletin)

vBulletin's parser is pretty complex (possibly needlessly?) compared to most regular expressions out there or drop-in-libraries. I'd honestly just dig through it, and take out what you can, since they tend to do things a little differently. I'd be surprised if you got a perfectly working parser without having to see how they actually generate/parse it themselves.

If it's old data, you may want to just write your own, but if it's old and new data coming in, why not just have it cached on the vBulletin side and use what they generate? Or just use the vB_BbCode_Parser class directly...

Hope this helps.

Adrian Schneider
I don't have access to the vBulletin parser, I'm handling BBCode from the forum though.
Josh K