最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
php完美过滤HTML代码的函数
时间:2022-06-24 15:46:48 编辑:袖梨 来源:一聚教程网
例子,使用strip_tags()函数过滤所有html
代码如下 | 复制代码 |
$str = ''; 输出结果为 <a href="#">href</a> href |
上面函数有一个问题就是包括html标签,img标签都过滤掉了,如果我们希望保留图片怎么办
在网上找到一个函数
代码如下 | 复制代码 |
function uh($str) |
这样就可以过滤指定标签了,上面方法还不会我们可参考下面办法
代码如下 | 复制代码 |
$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( |
如果只要过滤过滤html标签,js代码,css样式标签
代码如下 | 复制代码 |
|
这样即可了哦。
相关文章
- 向僵尸开炮尘世巨蟒怎么打-尘世巨蟒打法攻略 09-14
- 原神恰斯卡四星武器怎么搭配-原神恰斯卡四星武器搭配攻略 09-14
- 《勇者斗恶龙3重制版》徽章兑换方法及奖励列表 小徽章有什么用 09-14
- 星露谷物语雕刻南瓜任务怎么过-星露谷物语雕刻南瓜任务攻略 09-14
- 原神恰斯卡怎么培养-原神恰斯卡培养攻略 09-14
- 不一样传说2符文之语配方-不一样传说2符文之语配方汇总一览 09-14