BB Code Parser

I’ve been looking for a good BBCode parser, and never really found any good ones that were small, easy to deploy, and worked properly. They were either really large, required the whole class deal, or just didn’t work right. All the str_replace method ones don’t account for non closed tags, so an open [b] will amke the rest of the page bold. Some of the preg_replace method ones used something like [b](.*)[/b], which something like
[b]bold[/b] not bold [b]bold again[/b]
will evaluate to
bold[/b] not bold [b]bold again
Other parsers used (.+?) which will work for multiple tags, but not nested tags, like
[b]bold[b]still bold[/b][/b]
would evaluate to
This is fixed by adding a while loop checking preg_match of the regex.

Anyways, I broke down and wrote my own. It correctly parses everything I’ve thrown at it without any vulnerabilities, like javascript: uris, that other parsers had problems with.

Demo of BB Code Parser

Download as single txt file
Download in ZIP format

One thought on “BB Code Parser”

Leave a Reply

Your email address will not be published. Required fields are marked *