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