去掉html标签和空格等

时间:2023-03-09 22:18:51
去掉html标签和空格等

<?php
$str = '<span style="color:#f00;">good;&nbsp;world</span>';
echo $str.'<br>';
$str = preg_replace("/&[a-z]+\;/i",'',$str);
echo strip_tags($str);

 //替换字符
function replaceFilterHtml($str, $filterStr = '') {
$str=preg_replace("/\s+/", " ", $str); //过滤多余回车
$str=preg_replace("/<[ ]+/si","<",$str); //过滤<__("<"号后面带空格)
$str=preg_replace("/<\!–.*?–>/si","",$str); //注释
$str=preg_replace("/<(\!.*?)>/si","",$str); //过滤DOCTYPE $replaceArr = array('html', 'div', 'b', 'br', 'p','h1','h2','h3','h4','h5','h6','head','meta','body','link','form','applet','style','title','object','noframes','javascript','vbscript','img','span','strong','ui','em','pre','li','ul','dd','dl','dt','ol'); if($key = array_search($filterStr, $replaceArr)) {
unset($replaceArr[$key]);
} $replaceStr = implode('|', $replaceArr); $str=preg_replace("/<(\/?(".$replaceStr.").*?)>/si","",$str); //过滤html标签
$str=preg_replace("/cookie/si","COOKIE",$str); //过滤COOKIE标签
$str=preg_replace("/on([a-z]+)\s*=/si","On\\1=",$str); //过滤script标签
$str=preg_replace("/&#/si","&#",$str); //过滤script标签,如javAsCript:alert
return $str;
}