Jump to content

Yet Another BBCode Parser


RobertP

Recommended Posts

so i am creating a bbcode parser, and i am having a little issue with my pattern.

 

#\[([b|i|u])]((?:[^[]|\[(?!/?\1])|(?R))+)\[/\1]#i

 

this works for b,i & u tags; however i am trying to implement a tag that is longer then 1 character (not sure why this would cause a problem, but it does) and add a parameter.

 

i have tried the pattern below, but without luck.

#\[([b|i|u|color])(?:=(.+))]((?:[^[]|\[(?!/?\1])|(?R))+)\[/\1]#i

 

if someone could point me in the right direction, that would be amazing..

 

Link to comment
https://forums.phpfreaks.com/topic/273889-yet-another-bbcode-parser/
Share on other sites

Inside character sets ([...]) only individual characters matter. What you have now actually allows for a [|]...[/|] tag that I'm sure you don't want.

Use regular parentheses for grouping, which you already have in place.

 

Plus you've set it up so that [=x] is a valid tag.

 

In every implementation of BBCode I've seen there are individual rules for every single tag to replace. One for bold, one for italic, one for underline, and one for color. Gives you a lot more control over how they're handled too, and the code to do it is simpler to read and understand.

Thank you, i will use your recommendation, as it seems like common sense rather then trying to build each tag into one expression.

 

$patterns = array(
    '#\[(b)\]((?:[^[]|\[(?!/?\1])|(?R))+)\[/\1]#i',
    '#\[(i)\]((?:[^[]|\[(?!/?\1])|(?R))+)\[/\1]#i',
    '#\[(u)\]((?:[^[]|\[(?!/?\1])|(?R))+)\[/\1]#i',
    '#\[(color)\=(.+)]((?:[^[]|\[(?!/?\1])|(?R))+)\[/\1]#i',
);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.