Jump to content


Photo

Yet Another BBCode Parser

php bbcode regex

  • Please log in to reply
3 replies to this topic

#1 RobertP

RobertP

    Advanced Member

  • Members
  • PipPipPip
  • 288 posts

Posted 31 January 2013 - 09:19 PM

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

u tha king Pikachu2000!!

#2 RobertP

RobertP

    Advanced Member

  • Members
  • PipPipPip
  • 288 posts

Posted 31 January 2013 - 09:42 PM

ok, so i have the color working, (c tag in this example, as i still can not use tags that are longer then 1 character :( )

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

but, this has broken my other 3 tags, [b|i|u]
u tha king Pikachu2000!!

#3 requinix

requinix

    Transforming Moderator

  • Moderators
  • 6,128 posts
  • LocationWA

Posted 31 January 2013 - 09:52 PM

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, 31 January 2013 - 09:53 PM.


#4 RobertP

RobertP

    Advanced Member

  • Members
  • PipPipPip
  • 288 posts

Posted 31 January 2013 - 10:07 PM

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

u tha king Pikachu2000!!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com