博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
html编辑及JS脚本过滤(优化版)
阅读量:3603 次
发布时间:2019-05-20

本文共 1709 字,大约阅读时间需要 5 分钟。

这是参考CSDN上高手们的过滤方法,我修改后的方法

就是把  ""  换成string.Empty  从理论上(当数据多的时候)要快点

 

  1. public string NoHTML(string Htmlstring) //去除HTML标记 
  2.     { 
  3.         //删除脚本 
  4.         Htmlstring = Regex.Replace(Htmlstring, @" <script[^>]*?>.*? </script>",string.Empty, RegexOptions.IgnoreCase); 
  5.         //删除HTML 
  6.         Htmlstring = Regex.Replace(Htmlstring, @" <(.[^>]*)>",string.Empty, RegexOptions.IgnoreCase); 
  7.         Htmlstring = Regex.Replace(Htmlstring, @"([/r/n])[/s]+",string.Empty, RegexOptions.IgnoreCase); 
  8.         Htmlstring = Regex.Replace(Htmlstring, @"-->",string.Empty, RegexOptions.IgnoreCase); 
  9.         Htmlstring = Regex.Replace(Htmlstring, @" <!--.*",string.Empty, RegexOptions.IgnoreCase); 
  10.         Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);""/"", RegexOptions.IgnoreCase); 
  11.         Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);""&", RegexOptions.IgnoreCase); 
  12.         Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);"" <", RegexOptions.IgnoreCase); 
  13.         Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);"">", RegexOptions.IgnoreCase); 
  14.         Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);"," ", RegexOptions.IgnoreCase); 
  15.         Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);""/xa1", RegexOptions.IgnoreCase); 
  16.         Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);""/xa2", RegexOptions.IgnoreCase); 
  17.         Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);""/xa3", RegexOptions.IgnoreCase); 
  18.         Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);""/xa9", RegexOptions.IgnoreCase); 
  19.         Htmlstring = Regex.Replace(Htmlstring, @"&#(/d+);",string.Empty, RegexOptions.IgnoreCase); 
  20.         Htmlstring.Replace(" <",string.Empty); 
  21.         Htmlstring.Replace(">",string.Empty); 
  22.         Htmlstring.Replace("/r/n",string.Empty); 
  23.         Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim(); 
  24.         return Htmlstring; 
  25.     }

转载地址:http://pyizn.baihongyu.com/

你可能感兴趣的文章
剑指offer 47. 求1 + 2 + 3 + .... + n
查看>>
分布式和集群的区别
查看>>
本科毕设完整流程和注意事项
查看>>
不要想着憋大招,先完成每个小招
查看>>
ps中怎么把一张图片的一种颜色全部替换成另外一种颜色
查看>>
答辩PPT撰写和答辩注意事项
查看>>
第二次实习的实习总结
查看>>
PPT的一些常用操作
查看>>
Java面试题解析(基础篇,附答案)
查看>>
Spring 常用的三种注入方式
查看>>
MyBatis 一级缓存在分布式下的坑你知道吗?
查看>>
2020年2月JVM面试的30个知识点
查看>>
在家办公7天整理Spring Cloud知识点大全
查看>>
看看这些Java代码开发规范吧!你好,我好,大家好!
查看>>
2020年3月,47个经典Spring面试题详解(附带答案)
查看>>
python实现Mapreduce的wordcount
查看>>
Linux字符设备驱动编(步骤,框架(面向对象),分层)
查看>>
linux高级字符驱动之输入子系统
查看>>
代理与反射
查看>>
面向对象
查看>>