好的,所以我想我找到了一种方法,基于我在上面发布的链接的 explode_tags 函数的修改版本:
function explode_tags($chr, $str) {
for ($i=0, $j=0; $i < strlen($str); $i++) {
if ($str{$i} == $chr) {
while ($str{$i+1} == $chr) $i++;
$j++;
continue;
}
if ($str{$i} == "<") {
if (strlen($res[$j]) > 0) $j++;
$s = strpos($str, " ", $i);
$b = strpos($str, ">", $i);
if($s<$b) $end = $s;
else $end = $b;
$t = substr($str, $i+1, $end-$i-1);
$tend = strpos($str, ">", $i);
$tclose = strpos($str, "</".$t, $tend);
if($tclose!==false) $pos = strpos($str, ">", $tclose);
else $pos = strpos($str, ">", $i);
$res[$j] .= substr($str, $i, $pos - $i+1);
$i += ($pos - $i);
$j++;
continue;
}
if ((($str{$i} == "\n") || ($str{$i} == "\r")) && (strlen($res[$j]) == 0)) continue;
$res[$j] .= $str{$i};
}
return $res;
}
function filter_tags($content, $tags) {
$content = strip_tags($content, $tags);
$tags = substr($tags, 1, -1);
$d = strpos($tags, "><");
if($d===false) $tags = array($tags);
else $tags = explode("><", $tags);
$content = explode_tags("", $content);
$result="";
foreach($content as $c) {
$s = strpos($c, " ");
$b = strpos($c, ">");
if($s<$b) $end = $s;
else $end = $b;
$tag = substr($c, 1, $end-1);
if(in_array($tag, $tags)) $result.=$c;
}
return $result;
}
filter_tags($content, "<img><div><object><embed><iframe><param><script>");
到目前为止,这似乎工作得很好,尽管我只在几个不同的内容上尝试过。我不擅长这个,所以如果有人有建议,请自由分享......
感谢您的所有回答!