Nested bbCode

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Nested bbCode

Post by superdezign »

I created a nice little bbCode pattern that I'm using to make my blog writing simpler and enable the ability for me to make some patterns for parsing my code, but I've run into a problem.

Nested tags.

Any tags within other tags are completely ignored. I could run the preg_match_all on the results also, but that seems... unorthodox to me. Does anyone know how this would be accomplished, or is that the only way?

By the way, in case anyone's interested, here's the regex pattern thus far:

Code: Select all

(\[([\w]+)(=([\w]+))?\])(.*?)(\[\/\2\])
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

preg_match_all() the individual tags instead of them as a whole.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

How would I determine when the bbCode ended, then?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Maintain nesting counters.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Ooh.

But if I did that, then why not preg_match_all the innards of other tags, and keep a counter that way?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You wouldn't capture the entire nest. You'd capture the beginning and up to the first nested closing.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Gotcha. Back to work.
Post Reply