Jump to content


Photo

Simplified BBphp question on tag matching...


  • Please log in to reply
3 replies to this topic

#1 yungbloodreborn

yungbloodreborn
  • Members
  • PipPipPip
  • Advanced Member
  • 45 posts
  • LocationCalifornia

Posted 26 September 2006 - 06:53 PM

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:

[b] blah [u] blah [/b] blah [/u] 

or 

[b] blah [u] blah [/b] 

I've been using preg instead of ereg, if it makes any difference.

-YB

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 27 September 2006 - 08:25 AM

How are you passing your tags at the moment?

I allows parse my tags in pairs:
<?php

function 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;

?>
That way if there are any stray [b], [i], [u] or whatever tags are not closed 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.

#3 printf

printf
  • Staff Alumni
  • Advanced Member
  • 889 posts

Posted 29 September 2006 - 11:29 AM

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!

#4 yungbloodreborn

yungbloodreborn
  • Members
  • PipPipPip
  • Advanced Member
  • 45 posts
  • LocationCalifornia

Posted 29 September 2006 - 07:40 PM

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users