Topic: function to close tags works too well

I'm using a function based on this one to close open tags when truncating a post. It works great, too well in fact. It closes all tags, not just the ones which aren't closed.

The post if first parsed using the PunBB parse_message function then truncated to x characters and then close_tags is called.

This is beyond my understanding, if anyone can see why it's closing all tags, not just the open ones, it would be much appreciated.

function close_tags($string)
{
  // match opened tags
  if(preg_match_all('/<([a-z\:\-]+)[ >]/', $string, $start_tags))
  {
    $start_tags = $start_tags[1];

    // match closed tags
    if(preg_match_all('/<\/>([a-z]+)>/', $string, $end_tags))
    {
      $complete_tags = array();
      $end_tags = $end_tags[1];
    
      foreach($start_tags as $key => $val)
      {   
        $posb = array_search($val, $end_tags);
        if(is_integer($posb))
        {
          unset($end_tags[$posb]);
        }
        else
        {
          $complete_tags[] = $val;
        }
      }
    }
    else
    {
      $complete_tags = $start_tags;
    }
    
    $complete_tags = array_reverse($complete_tags);
    for($i = 0; $i < count($complete_tags); $i++)
    {
      $string .= '</' . $complete_tags[$i] . '>';
    }
  }
  // Removes the close tag for xhtml tags
$xhtml_tags = array("</img>", "</hr>", "</br>");
$string = str_replace($xhtml_tags, "", $string);
  return $string;
}

Re: function to close tags works too well

Never mind. Got it working. Now my blog mod is fully xhtml compliant smile

Re: function to close tags works too well

What is the difference with this one as oppose to the existing code in include/blog.php?

Re: function to close tags works too well

Hey bingiman, I saw your post on my site before I saw it here. A slight change to include/blog.php is the difference. You can download it here for now. Just overwrite the file and you won't have tags closed twice. It's a minor issue so I won't put it on punres until I get some other stuff done.