Josh K
Josh K

Reputation: 28883

Parse vBulletin's BB Code in PHP

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.

Upvotes: 4

Views: 2447

Answers (5)

Adrian Schneider
Adrian Schneider

Reputation: 7449

(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.

Upvotes: 1

bob-the-destroyer
bob-the-destroyer

Reputation: 3154

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

Upvotes: 0

Artefacto
Artefacto

Reputation: 97835

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

Upvotes: 0

dgw
dgw

Reputation: 530

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.

Upvotes: 1

mqchen
mqchen

Reputation: 4193

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.

Upvotes: 1

Related Questions