RobertP Posted February 1, 2013 Share Posted February 1, 2013 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.. Quote Link to comment https://forums.phpfreaks.com/topic/273889-yet-another-bbcode-parser/ Share on other sites More sharing options...
RobertP Posted February 1, 2013 Author Share Posted February 1, 2013 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] Quote Link to comment https://forums.phpfreaks.com/topic/273889-yet-another-bbcode-parser/#findComment-1409417 Share on other sites More sharing options...
requinix Posted February 1, 2013 Share Posted February 1, 2013 (edited) 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 February 1, 2013 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/273889-yet-another-bbcode-parser/#findComment-1409420 Share on other sites More sharing options...
RobertP Posted February 1, 2013 Author Share Posted February 1, 2013 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', ); Quote Link to comment https://forums.phpfreaks.com/topic/273889-yet-another-bbcode-parser/#findComment-1409423 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.