Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Apr 2004
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Starting/Ending Tags

    I was wondering how one would go about searching a string of input (such as in a forum) and make sure any tags started are ended before the end of the post. For example if someone starts a <b> tag i want to be sure that there is a corresponding </b> after it somewhere, and if not, inserting it in.

  • #2
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Would be best to use javascript for it. Then atleast the user wouldn't have to click back or whatever to change something.

  • #3
    Regular Coder
    Join Date
    Mar 2004
    Location
    Jackson, Georgia
    Posts
    102
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If you insist on using PHP for this then all you have to do is count the occurances of <b> and makes sure its the same ammount as </b>
    Designer's Toolz; Web & software development community.

    Get your coding questions answered:
    DTZ Forums

  • #4
    New to the CF scene
    Join Date
    Apr 2004
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well i had a reason I wanted it in php, so when the person posted they wouldn't have to worry about ending tags if they didn't want to, and when it displays the post it makes sure to end all the tags. I found a function that showed my how to do it though.


    function closeOpenTags($str, $open = "<", $close = ">", $end = "/", $tokens = "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
    {
    $chars = array();
    for ($i = 0; $i < strlen($tokens); $i++)
    {
    $chars[] = substr($tokens, $i, 1);
    }
    $openedTags = array();
    $closedTags = array();
    $tag = false;
    $closeTag = false;
    $tagName = "";
    for ($i = 0; $i < strlen($str); $i++)
    {
    $char = substr($str, $i, 1);
    if ($char == $open)
    { $tag = true; continue; }
    if ($char == $end)
    { $closeTag = true; continue; }
    if ($tag && in_array($char, $chars))
    { $tagName .= $char; }
    else
    { if ($closeTag)
    { if (isset($closedTags[$tagName]))
    { $closedTags[$tagName]++; }
    else
    { $closedTags[$tagName] = 1; } }
    elseif ($tag)
    { if (isset($openedTags[$tagName]))
    { $openedTags[$tagName]++; }
    else
    { $openedTags[$tagName] = 1; } }
    $tag = false; $closeTag = false; $tagName = ""; } }

    while(list($tag, $count) = each($openedTags))
    { $closedTags[$tag] = isset($closedTags[$tag]) ? $closedTags[$tag] : 0;
    $count -= $closedTags[$tag];
    if ($count < 1) continue;
    $str .= str_repeat($open.$end.$tag.$close, $count); }

    return $str;
    }


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •