当前位置:编程学习 > php >>

PHP用preg_replace过滤HTML标签 php过滤去除script标签

完美过滤script标签, PHP过滤HTML标签

$html = preg_replace('/<script[\s\S]*?<\/script>/i', '', $html);

PHP用preg_replace过滤HTML标签
 
<?php 
$str=$n['content'];
$str=str_replace('&nbsp;',"",$str);
$str=str_replace('$Next$',"",$str);
$str=str_replace('&lt;p&gt;',"",$str);
$str=str_replace('&lt;/p&gt;',"",$str);
$str = preg_replace('~<([abcdeghfontpzyu]+?)\s+?.h+?.*?>~i','<$1>',$str);
$tag='b' 
$p="/(<(?:\/".$tag."|".$tag.")[^>]*>)/i" 
$return_str = str_replace($p,"",$str);
echo substr($return_str,0,120);
?> 
 
php用preg_replace 过滤字符串,过滤特殊符号,script脚本

function guolv($str){
$str = preg_replace( "@<script(.*?)</script>@is", "", $str );
$str = preg_replace( "@<iframe(.*?)</iframe>@is", "", $str );
$str = preg_replace( "@<style(.*?)</style>@is", "", $str );
$str = preg_replace( "@<(.*?)>@is", "", $str );
//# 代表换行 
$str =str_replace("#","<br>",$str);
return $str 

}



正则表达式备注:

// .* 不能匹配换行

// [\s\S]* 支持匹配换行
// ? 表示匹配最近的一个结束标签</script>

// i 不区分大小写


其他人用php过滤标签的源代码:

$zzzyk.com=preg_replace("/<s*imgs+[^>]*?srcs*=s*('|")(.*?)\1[^>]*?/?s*>/i","", $zzzyk.com); //过滤img标签
$zzzyk.com=preg_replace("/s+/","", $zzzyk.com); //过滤多余回车
$zzzyk.com=preg_replace("/<[ ]+/si","<",$zzzyk.com); //过滤<__("<"号后面带空格)
$zzzyk.com=preg_replace("/<!--.*?-->/si","",$zzzyk.com); //注释
$zzzyk.com=preg_replace("/<(!.*?)>/si","",$zzzyk.com); //过滤DOCTYPE
$zzzyk.com=preg_replace("/<(/?html.*?)>/si","",$zzzyk.com); //过滤html标签
$zzzyk.com=preg_replace("/<(/?head.*?)>/si","",$zzzyk.com); //过滤head标签
$zzzyk.com=preg_replace("/<(/?meta.*?)>/si","",$zzzyk.com); //过滤meta标签
$zzzyk.com=preg_replace("/<(/?body.*?)>/si","",$zzzyk.com); //过滤body标签
$zzzyk.com=preg_replace("/<(/?link.*?)>/si","",$zzzyk.com); //过滤link标签
$zzzyk.com=preg_replace("/<(/?form.*?)>/si","",$zzzyk.com); //过滤form标签
$zzzyk.com=preg_replace("/cookie/si","COOKIE",$zzzyk.com); //过滤COOKIE标签
$zzzyk.com=preg_replace("/<(applet.*?)>(.*?)<(/applet.*?)>/si","",$zzzyk.com); //过滤applet标签
$zzzyk.com=preg_replace("/<(/?applet.*?)>/si","",$zzzyk.com); //过滤applet标签
$zzzyk.com=preg_replace("/<(style.*?)>(.*?)<(/style.*?)>/si","",$zzzyk.com); //过滤style标签
$zzzyk.com=preg_replace("/<(/?style.*?)>/si","",$zzzyk.com); //过滤style标签
$zzzyk.com=preg_replace("/<(title.*?)>(.*?)<(/title.*?)>/si","",$zzzyk.com); //过滤title标签
$zzzyk.com=preg_replace("/<(/?title.*?)>/si","",$zzzyk.com); //过滤title标签
$zzzyk.com=preg_replace("/<(object.*?)>(.*?)<(/object.*?)>/si","",$zzzyk.com); //过滤object标签
$zzzyk.com=preg_replace("/<(/?objec.*?)>/si","",$zzzyk.com); //过滤object标签
$zzzyk.com=preg_replace("/<(noframes.*?)>(.*?)<(/noframes.*?)>/si","",$zzzyk.com); //过滤noframes标签
$zzzyk.com=preg_replace("/<(/?noframes.*?)>/si","",$zzzyk.com); //过滤noframes标签
$zzzyk.com=preg_replace("/<(i?frame.*?)>(.*?)<(/i?frame.*?)>/si","",$zzzyk.com); //过滤frame标签
$zzzyk.com=preg_replace("/<(/?i?frame.*?)>/si","",$zzzyk.com); //过滤frame标签
$zzzyk.com=preg_replace("/<(script.*?)>(.*?)<(/script.*?)>/si","",$zzzyk.com); //过滤script标签
$zzzyk.com=preg_replace("/<(/?script.*?)>/si","",$zzzyk.com); //过滤script标签
$zzzyk.com=preg_replace("/javascript/si","Javascript",$zzzyk.com); //过滤script标签
$zzzyk.com=preg_replace("/vbscript/si","Vbscript",$zzzyk.com); //过滤script标签
$zzzyk.com=preg_replace("/on([a-z]+)s*=/si","On\1=",$zzzyk.com); //过滤script标 
$zzzyk.com=preg_replace("/&#/si","&#",$zzzyk.com); //过滤script标签


CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,