Remove Empty html tags from string - PHP
by Subramanian[ Edit ] 2014-05-26 14:55:47
The below function is used to remove empty html tags from html content.
Note : This function is only removed the empty html tag without any attribute.
Example :
1.<p>GvSubhu<p></p></p> => <p>GvSubhu</p>
2.<p>GvSubhu<p></p><a href="#"></a></p> => <p>GvSubhu</p><a href="#"></a>
Code :
function remove_empty_tags ($str)
{
if (!is_string ($str) || trim ($str) == '')
return $str;
return preg_replace ( '/<([^</>]*)>([s]*?|(?R))</1>/imsU', $repto, $str );
}
$str="<b>This is <p></p>GvSubhu</b><pre></pre>";
echo htmlentities(remove_empty_tags ($str));
Output :
<b>This is GvSubhu</b>