Blog Details

function to find and complete open html tags in php


                                
                                    <?php
function closeHTMLtags($html, $length)
{
$truncatedText = substr($html, $length);
$pos = strpos($truncatedText, ">");
if($pos !== false)
{
$html = substr($html, 0,$length + $pos + 1);
}
else
{
$html = substr($html, 0,$length);
}

preg_match_all('#<(?!meta|img|br|hr|input\b)\b([a-z]+)(?: .*)?(?<![/|/ ])>#iU', $html, $result);
$openedtags = $result[1];

preg_match_all('#</([a-z]+)>#iU', $html, $result);
$closedtags = $result[1];

$len_opened = count($openedtags);

if (count($closedtags) == $len_opened)
{
return $html;
}

$openedtags = array_reverse($openedtags);
for ($i=0; $i < $len_opened; $i++)
{
if (!in_array($openedtags[$i], $closedtags))
{
$html .= '</'.$openedtags[$i].'>';
}
else
{
unset($closedtags[array_search($openedtags[$i], $closedtags)]);
}
}


return $html;
}

$str = "This text has <u><b> bold text in it";
$strlength=strlen($str);
echo $str=closeHTMLtags($str,$strlength);
?>
                                    


Categories

Core_PHP

Latest posts

13.05.14
Implementing CRUD Operations ΒΆ

<ul> <li>Now comes the fun part. We would like t

13.05.14
View

<ol> <li><a href="http://www.yiiframework.com/do

13.05.14
Creating Model

<ol> <li><a href="http://www.yiiframework.com/do

Get Free Quote