Jump to content

Archived

This topic is now archived and is closed to further replies.

yungbloodreborn

Simplified BBphp question on tag matching...

Recommended Posts

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

Share this post


Link to post
Share on other sites
How are you passing your tags at the moment?

I allows parse my tags in pairs:
[code]<?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;

?>[/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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.