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

Edited by requinix
Link to comment
Share on other sites

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',
);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.