首页 > 编程知识 正文

php过虑html标签,php过滤html标签

时间:2023-12-29 20:32:09 阅读:331297 作者:TPES

本文目录一览:

php如何过滤编辑器的html标签?

选择1.将特殊符号进行转换,可以用htmlspecialchars把变为“”等

选择2.用正则表达式替换,将标签都删除:

$content=preg_replace('/.+?/','',$content);

用php过滤html部分标签

$str=preg_replace("/s+/", " ", $str); //过滤多余回车

$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("rn","",$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标签的正则表达式的枚举式(比如需要过滤apb等):

复制代码 代码如下:

/?[aApPbB][^]*

5,过滤部分html标签的属性的正则表达式的排除式(比如排除alt属性,即不过滤alt属性):

复制代码 代码如下:

s(?!alt)[a-zA-Z]+=[^s]*

6,过滤部分html标签的属性的正则表达式的枚举式(比如alt属性):

复制代码 代码如下:

(s)alt=[^s]*

php如何过滤html标签,使用什么函数?

strip_tags — 从字符串中去除 HTML 和 PHP 标记

语法:

string strip_tags ( string $str [, string $allowable_tags ] )

该函数返回给定的字符串 str 去除空字符、HTML 和 PHP 标记后的结果。

参数:

str 要去除的字符串

allowable_tags 可选参数,指定不被去除的字符列表。

例如:

$str = 'a href="" title=""测试/a';

echo strip_tags($str);

结果:

测试

PHP如何去除HTML标签

PHP 中有个 strip_tags 函数可以方便地去除 HTML 标签。

echo strip_tags(“Hello bWorld/b”); // 去除 HTML、XML 以及 PHP 的标签。

对于非标准的 HTML 代码也能正确的去除:

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。