i have a page where users put their own posts, users are able to markup their posts with tags like :
{strong}{/strong} or {italic}{/italic} or {title}{/title} etc ...
the difficult for me is to make them not invading between themself.
i mean this is ok:
{strong}{/strong} {italic}{/italic}
but i need to avoid all the possible cases like these:
{strong}{italic}{/strong}{/italic}
{italic}{strong}{/italic}{italic}{/strong}{/italic}
{italic}{/strong}{/italic}{/strong}{/italic}
and so on … really too much cases to write 1 control foreach one i think 😛
the logic should be to make them always separated and remove the not needed or included tags … hoping question is clear 😛
The general solution to your problem is to develop a recursive descent parser or a stack based parser, but is probably complete overkill for your situation.
Matching start tags with end tags is in general very similar to the language of balanced parentheses (for instance: (), (()), (()()) are balanced correctly, )(, ()(, )(()() are not). The language of balanced parentheses is not a regular language, and therefore cannot be ‘parsed’ using regular expressions (unless you can limit the depth that they can be nested, see here).
My suggestion to you would be to convert all of your pseudo tags into actual html tags, and then parse that with PHP’s Document Object Model.