$str=preg_replace("/<[ ]+/si","<",$str)//过滤<__("<"号后面带空格)
$str=preg_replace("/<\!--.*?-->/si","",$str)//注释
$str=preg_replace("/<(\!.*?)>/si","",$str)//过滤DOCTYPE
$str=preg_replace("/<(\/?html.*?)>/si","",$str)//过滤html标签
$str=preg_replace("/<(\/?head.*?)>/si","",$str)//过滤head标签
$str=preg_replace("/<(\/?meta.*?)>/si","",$str)//过滤meta标签
$str=preg_replace("/<(\/?body.*?)>/si","",$str)//过滤body标签
$str=preg_replace("/<(\/?link.*?)>/si","",$str)//过滤link标签
$str=preg_replace("/<(\/?form.*?)>/si","",$str)//过滤form标签
$str=preg_replace("/cookie/si","COOKIE",$str)//过滤COOKIE标签
$str=preg_replace("/<(applet.*?)>(.*?)<(\/applet.*?)>/si","",$str)//过滤applet标签
$str=preg_replace("/<(\/?applet.*?)>/si","",$str)//过滤applet标签
$str=preg_replace("/<(style.*?)>(.*?)<(\/style.*?)>/si","",$str)//过滤style标签
$str=preg_replace("/<(\/?style.*?)>/si","",$str)//过滤style标签
$str=preg_replace("/<(title.*?)>(.*?)<(\/title.*?)>/si","",$str)//过滤title标签
$str=preg_replace("/<(\/?title.*?)>/si","",$str)//过滤title标签
$str=preg_replace("/<(object.*?)>(.*?)<(\/object.*?)>/si","",$str)//过滤object标签
$str=preg_replace("/<(\/?objec.*?)>/si","",$str)//过滤object标签
$str=preg_replace("/<(noframes.*?)>(.*?)<(\/noframes.*?)>/si","",$str)//过滤noframes标签
$str=preg_replace("/<(\/?noframes.*?)>/si","",$str)//过滤noframes标签
$str=preg_replace("/<(i?frame.*?)>(.*?)<(\/i?frame.*?)>/si","",$str)//过滤frame标签
$str=preg_replace("/<(\/?i?frame.*?)>/si","",$str)//过滤frame标签
$str=preg_replace("/<(script.*?)>(.*?)<(\/script.*?)>/si","",$str)//过滤script标签
$str=preg_replace("/<(\/?script.*?)>/si","",$str)//过滤script标签
$str=preg_replace("/javascript/si","Javascript",$str)//过滤script标签
$str=preg_replace("/vbscript/si","Vbscript",$str)//过滤script标签
$str=preg_replace("/on([a-z]+)\s*=/si","On\\1=",$str)//过滤script标签
$str=preg_replace("//si","&#",$str)//过滤script标签,如javAsCript:alert(
清除空格,换行
function DeleteHtml($str)
{
$str = trim($str)
$str = strip_tags($str,"")
$str = ereg_replace("\t","",$str)
$str = ereg_replace("\r\n","",$str)
$str = ereg_replace("\r","",$str)
$str = ereg_replace("\n","",$str)
$str = ereg_replace(" "," ",$str)
return trim($str)
}
过滤HTML属性
1,过滤所有html标签的正则表达式:
复制代码 代码如下:
</?[^>]+>
//过滤所有html标签的属性的正则表达式:
$html = preg_replace("/<([a-zA-Z]+)[^>]*>/","<\\1>",$html)
3,过滤部分html标签的正则表达式的排除式(比如排除<p>,即不过滤<p>):
复制代码 代码如下:
</?[^pP/>]+>
4,过滤部分html标签的正则表达式的枚举式(比如需要过滤<a><p><b>等):
复制代码 代码如下:
</?[aApPbB][^>]*>
5,过滤部分html标签的属性的正则表达式的排除式(比如排除alt属性,即不过滤alt属性):
复制代码 代码如下:
\s(?!alt)[a-zA-Z]+=[^\s]*
6,过滤部分html标签的属性的正则表达式的枚举式(比如alt属性):
复制代码 代码如下:
(\s)alt=[^\s]*
这函数挺有用的,这是总结后的知识点,希望能帮到你!strip_tags
(PHP 4, PHP 5, PHP 7, PHP 8)
strip_tags — 从字符串中去除 HTML 和 PHP 标记
说明
strip_tags ( string $str , string $allowable_tags = ? ) : string
该函数尝试返回给定的字符串 str 去除空字符、HTML 和 PHP 标记后的结果。它使用与函数 fgetss() 一样的机制去除标记。
参数
str
输入字符串。
allowable_tags
使用可选的第二个参数指定不被去除的字符列表。
注意:
HTML 注释和 PHP 标签也会被去除。这里是硬编码处理的,所以无法通过 allowable_tags 参数进行改变。
注意:
In PHP 5.3.4 and later, self-closing XHTML tags are ignored and only non-self-closing tags should be used in allowable_tags. For example, to allow both
and , you should use:')
?>
返回值
返回处理后的字符串。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)