Jump to content

Simplified BBphp question on tag matching...


yungbloodreborn

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.