【发布时间】:2010-12-19 21:21:00
【问题描述】:
从字符串中删除空 html 标记的最快方法是什么?
我编写了这样的程序来检测空锚标签:
$temp = strip_tags($string, "<blockquote><a>");
$cmatch = array();
if(preg_match_all("~<a.*><\/a>~iU", $temp, $cmatch, PREG_SET_ORDER))
{
foreach($cmatch as $cm)
{
foreach($cm as $t) //echo htmlentities($t)."<br />";
$temp = trim(str_replace($t, '', $temp));
}
}
if(!empty($temp))
{
echo '<div class="c" style="margin-top:20px;">';
echo $temp;
echo '</div>';
}
//do not output if empty tags (problem with div margin)
必须有可能更有效地做到这一点。将字符串转换为 html DOM 并在那里进行检查会更快吗?
【问题讨论】:
标签: php html string tags anchor