先记下来,以作备用!

///<summary>去除HTML标记//////</summary>///<param name="Htmlstring">包括HTML的源码</param>///<returns>已经去除后的文字</returns>publicstaticstringGetNoHTMLString(stringHtmlstring)
        {
//删除脚本Htmlstring=Regex.Replace(Htmlstring,@"<script[^>]*?>.*?</script>","", RegexOptions.IgnoreCase);//删除HTMLHtmlstring=Regex.Replace(Htmlstring,@"<(.[^>]*)>","", RegexOptions.IgnoreCase);
            Htmlstring
=Regex.Replace(Htmlstring,@"([\r\n])[\s]+","", RegexOptions.IgnoreCase);
            Htmlstring
=Regex.Replace(Htmlstring,@"-->","", RegexOptions.IgnoreCase);
            Htmlstring
=Regex.Replace(Htmlstring,@"<!--.*","", RegexOptions.IgnoreCase);

Htmlstring=Regex.Replace(Htmlstring,@"&(quot|#34);","\"", RegexOptions.IgnoreCase);Htmlstring=Regex.Replace(Htmlstring,@"&(amp|#38);","&", RegexOptions.IgnoreCase);
            Htmlstring
=Regex.Replace(Htmlstring,@"&(lt|#60);","<", RegexOptions.IgnoreCase);
            Htmlstring
=Regex.Replace(Htmlstring,@"&(gt|#62);",">", RegexOptions.IgnoreCase);
            Htmlstring
=Regex.Replace(Htmlstring,@"&(nbsp|#160);","", RegexOptions.IgnoreCase);
            Htmlstring
=Regex.Replace(Htmlstring,@"&(iexcl|#161);","\xa1", RegexOptions.IgnoreCase);
            Htmlstring
=Regex.Replace(Htmlstring,@"&(cent|#162);","\xa2", RegexOptions.IgnoreCase);
            Htmlstring
=Regex.Replace(Htmlstring,@"&(pound|#163);","\xa3", RegexOptions.IgnoreCase);
            Htmlstring
=Regex.Replace(Htmlstring,@"&(copy|#169);","\xa9", RegexOptions.IgnoreCase);
            Htmlstring
=Regex.Replace(Htmlstring,@"(\d+);","", RegexOptions.IgnoreCase);

Htmlstring.Replace("<","");
            Htmlstring.Replace(
">","");
            Htmlstring.Replace(
"\r\n","");
            Htmlstring
=HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();returnHtmlstring;
        }
///<summary>获取显示的字符串,可显示HTML标签,但把危险的HTML标签过滤,如iframe,script等。//////</summary>///<param name="str">未处理的字符串</param>///<returns></returns>publicstaticstringGetSafeHTMLString(stringstr)
        {
            str
=Regex.Replace(str,@"<applet[^>]*?>.*?</applet>","", RegexOptions.IgnoreCase);
            str
=Regex.Replace(str,@"<body[^>]*?>.*?</body>","", RegexOptions.IgnoreCase);
            str
=Regex.Replace(str,@"<embed[^>]*?>.*?</embed>","", RegexOptions.IgnoreCase);
            str
=Regex.Replace(str,@"<frame[^>]*?>.*?</frame>","", RegexOptions.IgnoreCase);
            str
=Regex.Replace(str,@"<script[^>]*?>.*?</script>","", RegexOptions.IgnoreCase);
            str
=Regex.Replace(str,@"<frameset[^>]*?>.*?</frameset>","", RegexOptions.IgnoreCase);
            str
=Regex.Replace(str,@"<html[^>]*?>.*?</html>","", RegexOptions.IgnoreCase);
            str
=Regex.Replace(str,@"<iframe[^>]*?>.*?</iframe>","", RegexOptions.IgnoreCase);
            str
=Regex.Replace(str,@"<style[^>]*?>.*?</style>","", RegexOptions.IgnoreCase);
            str
=Regex.Replace(str,@"<layer[^>]*?>.*?</layer>","", RegexOptions.IgnoreCase);
            str
=Regex.Replace(str,@"<link[^>]*?>.*?</link>","", RegexOptions.IgnoreCase);
            str
=Regex.Replace(str,@"<ilayer[^>]*?>.*?</ilayer>","", RegexOptions.IgnoreCase);
            str
=Regex.Replace(str,@"<meta[^>]*?>.*?</meta>","", RegexOptions.IgnoreCase);
            str
=Regex.Replace(str,@"<object[^>]*?>.*?</object>","", RegexOptions.IgnoreCase);returnstr;
        }

转载于:https://www.cnblogs.com/niunan/archive/2010/05/20/1739813.html

ASP.NET中过滤HTML字符串的两个方法相关推荐

  1. 在PHP代码中处理JSON 格式的字符串的两种方法:

    总结: 在PHP代码中处理JSON 格式的字符串的两种方法: 方法一: $json= '[{"id":"1","name":"\u ...

  2. 站长在线Python精讲:在Python中格式化字符串的两种方法详解

    欢迎你来到站长在线的站长学堂学习Python知识,本文学习的是<在Python中格式化字符串的两种方法详解>.本知识点主要内容有:使用%操作符格式化字符串和使用format()方法格式化字 ...

  3. C++ 区分中文,非中文,截取含有中文的string字符串的两种方法

    C++ 区分中文,非中文,截取含有中文的string字符串的两种方法 方法一 根据中文在ASCII中的范围判断 方法二 把string转成wstring 转自: http://blog.51cto.c ...

  4. Java中的string定义的两种方法和区别

    java中的String定义的两种方法和区别 第一种:new方式 String s1 = new String("hello world"); String s2 = new St ...

  5. VB中FSO的调用的两种方法

    方法一:   Dim   objFso      Set   objFso   =   CreateObject("Scripting.FileSystemObject")    ...

  6. .NET深入学习笔记(2):C#中判断空字符串的4种方法性能比较与分析

    偶然看到<C#中判断空字符串的3种方法性能分析 >作者:清清月儿  主页:http://blog.csdn.net/21aspnet/           时间:2007.4.28 写的一 ...

  7. new Date将字符串转化成日期格式 兼容IE,ie8如何通过new Date将字符串转化成日期格式,js中如何进行字符串替换, replace() 方法详解

    new Date将字符串转化成日期格式 兼容IE,ie8如何通过new Date将字符串转化成日期格式,js中如何进行字符串替换, replace() 方法详解 //获得年月日时分秒 //传入日期// ...

  8. python数据处理常用函数_Python中常用操作字符串的函数与方法总结

    Python中常用操作字符串的函数与方法总结 这篇文章主要介绍了Python中常用操作字符串的函数与方法总结,包括字符串的格式化输出与拼接等基础知识,需要的朋友可以参考下 例如这样一个字符串 Pyth ...

  9. php 带建数组转字符串,php数组转换为字符串的两种方法详解【附视频】

    本篇文章主要给大家介绍PHP数组转换为字符串的两种方法.(文章末尾附有对应的视频教程) 第一种方法:使用PHP本身的函数implode来直接将数组转换为字符串. 第二种方法:使用循环遍历数组元素拼接成 ...

最新文章

  1. VMware与Hyper-V
  2. java_ant详解(转载)
  3. 使用Ubuntu 12.04作为日常电脑环境
  4. 指针-数组传参,指针传参
  5. Lua语法入门HelloWorld
  6. Codeforces Round #401 (Div. 2) D. Cloud of Hashtags
  7. 系统之家win11最新旗舰版64位镜像v2021.07
  8. 《Kotlin从零到精通Android开发》资源下载和内容勘误
  9. HDU 1551 Cable master
  10. 第五章 编码/加密——《跟我学Shiro》[张开涛]
  11. mac开机启动项怎么设置
  12. 常用技术网站2017-2018
  13. Halcon实战之MLP颜色匹配(一)
  14. 2021-02-24 bat 批处理教程
  15. 微信表情的字符编号完整版【图文并茂哦 】
  16. 随机生成英文字母 php,PHP生成随机英文用户名
  17. java outer的使用
  18. Oracle中反GROUP BY的写法
  19. 惊闻广州政协做调查为广州电视台取消粤语节目做准备
  20. 阿里云部署项目详细步骤

热门文章

  1. vuex 源码分析_Vuex 2.0 源码分析(下)
  2. python词云图代码示例 无jieba_【词云图】如何用python的第三方库jieba和wordcloud画词云图...
  3. 服务器维修app是什么状态,监控服务器状态app
  4. es搜索热度属性_是不是场吊打局?奥迪A6L对比雷克萨斯ES
  5. 重启服务器之home下文件全没,小白宝典——树莓派实用工具分享(大神绕路)
  6. Jupyter Noteboot 添加kernel 环境
  7. windows 的mysql5.7安装
  8. 我用Python分析了“青你2”漂亮小姐姐的颜值,结果真香了!
  9. PyCharm 2020.1 稳定版发布
  10. mysql自动写入创建时间_mysql 自动记录数据插入及最后修改时间