首先分享一些比较常见的

$str=preg_replace("/<s*imgs+[^>]*?srcs*=s*(''|")(.*?)\1[^>]*?/?s*>/i","", $str); //过滤img标签

$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标签

更简单些的写法:

function delhtml($str){  //清除html标签
$st=-1; //开始
$et=-1; //结束
$stmp=array();
$stmp[]=" ";
$len=strlen($str);
for($i=0;$i<$len;$i++){
$ss=substr($str,$i,1);
if(ord($ss)==60){ //ord("<")==60
$st=$i;
}
if(ord($ss)==62){ //ord(">")==62
$et=$i;
if($st!=-1){
$stmp[]=substr($str,$st,$et-$st+1);
}
}
}
$str=str_replace($stmp,"",$str);
return $str;
}

再来一个:

function clear_html_label($html)
{
$search = array ("''<script[^>]*?>.*?</script>''si", "''<[/!]*?[^<>]*?>''si", "''([rn])[s]+''", "''&(quot|#34);''i", "''&(amp|#38);''i", "''&(lt|#60);''i", "''&(gt|#62);''i", "''&(nbsp|#160);''i", "''&(iexcl|#161);''i", "''&(cent|#162);''i", "''&(pound|#163);''i", "''&(copy|#169);''i", "''&#(d+);''e");
$replace = array ("", "", "1", """, "&", "<", ">", " ", chr(161), chr(162), chr(163), chr(169), "chr(1)");
return preg_replace($search, $replace, $html);
}

以上三种方法均可以实现,不过各有优劣,小伙伴们根据自己的项目需求来选择吧。

PHP实现过滤各种HTML标签的更多相关文章

  1. PHP通用的XSS攻击过滤函数,Discuz系统中 防止XSS漏洞攻击,过滤HTML危险标签属性的PHP函数

    XSS攻击在最近很是流行,往往在某段代码里一不小心就会被人放上XSS攻击的代码,看到国外有人写上了函数,咱也偷偷懒,悄悄的贴上来... 原文如下: The goal of this function ...

  2. 过滤字符串html标签方法

    过滤字符串html标签方法,如果输入的过滤标签为“*”,那么给字符串加上p标签 public static string noTagHtml(string str, string tagname) { ...

  3. 正则过滤html的标签

    $('#Text').find('br').remove();//移除br标签 let content = $('#smsText').html().replace(/ /g, ' ').replac ...

  4. java 使用正则表达式过滤HTML中标签

    /** * 去掉文本中的html标签 * * @param inputString * @return */ public static String html2Text(String inputSt ...

  5. PHP过滤各种HTML标签

    $str=preg_replace("/<\s*img\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i&q ...

  6. PHP过滤各种HTML标签的表达式,值得收藏

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  7. 百度UEditor富文本编辑器去除过滤div等标签

    将设计排版好的页面html代码上传到数据库,再读取出来的时候发现所有的div都被替换成了p标签. 解决方法: 首先在ueditor.all.js文件内搜索allowDivTransToP,找到如下的代 ...

  8. asp.net正则表达式提取网页网址、标题、图片实例以及过滤所有HTML标签实例

    无论你用什么语言,正则表达式的处理方法都是非常灵活.高效的,尤其是对某些字符串的抓取.过滤方面,更显其优势. 正则表达式的写法通常比较简单,几行短代码便能轻松完成看似很复杂的事情,更值得称赞的是,它的 ...

  9. php特殊字符过滤,html标签处理

    1,magic_quotes_gpc  默认情况下,PHP 指令 magic_quotes_gpc 为 on,对所有的 GET.POST 和 COOKIE 数据自动运行 addslashes().不要 ...

随机推荐

  1. 高效开发Android App的10个建议

    假如要Google Play上做一个最失败的案例,那最好的秘诀就是界面奇慢无比.耗电.耗内存.接下来就会得到用户的消极评论,最后名声也就臭了.即使你的应用设计精良.创意无限也没用. 耗电或者内存占用等 ...

  2. Servlet3.0学习总结——基于Servlet3.0的文件上传

    Servlet3.0学习总结(三)——基于Servlet3.0的文件上传 在Servlet2.5中,我们要实现文件上传功能时,一般都需要借助第三方开源组件,例如Apache的commons-fileu ...

  3. Android Studio 经常使用功能介绍

    为了简化 Android 的开发力度,Google 决定将重点建设 Android Studio 工具.Google 会在今年年底停止支持其它集成开发环境.比方 Eclipse. Android St ...

  4. 本博客停止更新,移至purecpp.org

    声明:本博客停止更新,迁移至C++开源社区purecpp.org, 欢迎持续关注!

  5. mysql隔离级别

    MySQL/InnoDB定义的4种隔离级别: Read Uncommited 可以读取未提交记录. Read Committed (RC) 针对当前读,RC隔离级别保证对读取到的记录加锁 (记录锁), ...

  6. PSAM卡与CPU(用户卡)的操作过程

    最近我一直在研究关于通过国密PSAM卡作为安全模块来读写cpu(用户卡)的问题 其实,两者都是卡片,或者说都是从设备.它们之间是无法相互访问的. 实际上,PSAM 卡是作为秘密密钥的载体,专门执行加密 ...

  7. sizeof and strlen整理

    sizeof 定义 计算对象或类型所占用的字节数(byte) 记住是字节数,而不是个数 语法 sizeof(对象) int i; sizeof(i); sizeof(类型) sizeof(int); ...

  8. 用 Python 通过马尔可夫随机场(MRF)与 Ising Model 进行二值图降噪

    前言 这个降噪的模型来自 Christopher M. Bishop 的 Pattern Recognition And Machine Learning (就是神书 PRML……),问题是如何对一个 ...

  9. MVC自定义视图规则

    自定义规则: using System.Web.Mvc; using System.Configuration; namespace Research { public class ViewConfi ...

  10. 当碰到unix纪元问题时strtotime怎么转时间戳(DateTime类的使用方法)

    UNIX纪元时间又称POSIX时间/新纪元时间(Epoch Time):从协调世界时1970年1月1日0时0分0秒起到现在的总秒数,不包括闰秒.正值表示1970以後,负值则表示1970年以前. Uni ...