yungbloodreborn Posted September 26, 2006 Share Posted September 26, 2006 Hey, I'm working on a simplified BBphp editor for my site. What is the easiest way to ensure there are no overlapping tags, or tags missing it's mate?I want to prevent stuff like this:[code][b] blah [u] blah [/b] blah [/u] or [b] blah [u] blah [/b] [/code]I've been using preg instead of ereg, if it makes any difference.-YB Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted September 27, 2006 Share Posted September 27, 2006 How are you passing your tags at the moment?I allows parse my tags in pairs:[code]<?phpfunction bbcode($txt){ // bbcodes $bbcodes = array( "|\[b\](.+)\[/b\]|is", "|\[u\](.+)\[/u\]|is", "|\[i\](.+)\[/i\]|is" ); // html $replace = array( "<strong>$1</strong>", "<u>$1</u>", "<em>$1</em>" ); $txt = preg_replace($bbcodes, $replace, $txt); return nl2br($txt);}$str = "[b]hey[/b] a [u][i]BBCode parser[/i][/u]! ";$str = bbcode($str);echo $str;?>[/code]That way if there are any stray [nobbc][b], [i], [u] or whatever tags are not closed[/nobbc] they can be seen. Then the author can remove the unmatched tag. or you can add code on to the function which remove any non matched tags. Quote Link to comment Share on other sites More sharing options...
printf Posted September 29, 2006 Share Posted September 29, 2006 If you want to handle overlapping invalid tags then you will need to use a stack loop, where you take a single starting tag, and perform a greedy match to the right most closing tag, then keep doing that for each tag you allow, on the text returned in that tag, then move on to the next tag doing the same thing. That's the only way to never replace a invalid overlapping tag!me! Quote Link to comment Share on other sites More sharing options...
yungbloodreborn Posted September 29, 2006 Author Share Posted September 29, 2006 wildteen, I saw that in the bbphp tutorial...and that does answer part of my question. Currently I'm not doing any tag handling. I plan on doing the tag handling in the page the displays the message, not the page that writes the message.printf, I admit, I am an amatuer at PHP. do you think you could show me a sample piece of code so I can see what you mean? That sounds like what I had in mind, and I could easily do it in c++, but I don't know all the string handling in php. And I really need to learn more about regex... I've never learned them in detail... Quote Link to comment 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.