5

I'm looking for a BBCode parser in Javascript or PHP without the need of using Regex. Can anybody suggest me a good one?

Teiv
  • 2,605
  • 10
  • 39
  • 48
  • 2
    why are you avoiding regex? (speed?) – tofutim May 25 '11 at 17:38
  • 3
    Because I don't just want to replace the tags, I also want to do a little bit with the content between the tags. Do you have any suggestion for me? – Teiv May 25 '11 at 18:11
  • 1
    "I also want to do a little bit with the content between the tags" `preg_replace_callback()` is great for this. If you can give us a solid example of what exactly you're trying to do, we can give you more specific advice. – Frank Farmer May 26 '11 at 22:10

6 Answers6

6

It is recommended to use regex.

Other solution:

function bb_parse($str)
{
    return str_replace(array('[b]', '[/b]'), array('<strong>', '</strong>'), $str);
}

This can break parsing due to mis-closing tags can end up content being wrapped with a HTML tag without closing.

MacMac
  • 34,294
  • 55
  • 151
  • 222
  • Ehm, BB is a recursive format just as HTML. Thus, I invoke the pony: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – Prof. Falken Apr 20 '12 at 08:40
2

There's a PECL extension for bbcode. You'll need to take a look on how to install PECL extensions in order to utilize it.

onteria_
  • 68,181
  • 7
  • 71
  • 64
1

Zend parser might be what you're looking for http://framework.zend.com/manual/en/zend.markup.parsers.html

Unfortunately, I found it the least practically functional of the BBCode parsers I evaluated: when encountering malformed markup ([b] asdf [/ wops I forgot to close my tag) it tends to throw away all content after the first malformed tag. Other bbcode parsers do a much better job of simply ignoring bad markup.

Frank Farmer
  • 38,246
  • 12
  • 71
  • 89
0

I recently write a bbcode parser in javascript.

What it can do:

  1. Convert BBcode string to HTML string;
  2. Convert HTML element to BBCode string;
  3. Auto correct BBCode string;

Check the demo: UBBParser

Zachary
  • 257
  • 2
  • 7
0

So I know you said no regex, but I recently wrote a BBCode parser in JavaScript, and I believe it addresses your concerns since it is not a simple find and replace and it gives you access to the content within the tags. You can see a demo of it here:

http://patorjk.com/bbcode-previewer/

And get the source and write up on it here:

http://patorjk.com/blog/2011/05/07/extendible-bbcode-parser-in-javascript/

patorjk
  • 2,164
  • 1
  • 20
  • 30
-1

If you can install a PECL extension, you will be able to use the BBCode functions

BenMorel
  • 34,448
  • 50
  • 182
  • 322