asp.net 字符串过滤
/// <summary>
/// 去除HTML标记
/// </summary>
/// <param name="Htmlstring">包括HTML的源码</param>
/// <returns>已经去除后的文字</returns>
public static string GetNoHTMLString(string Htmlstring)
{
//删除脚本
Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
//删除HTML
Htmlstring = 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(); return Htmlstring;
}
/// <summary>
/// 获取显示的字符串,可显示HTML标签,但把危险的HTML标签过滤,如iframe,script等。
/// </summary>
/// <param name="str">未处理的字符串</param>
/// <returns></returns>
public static string GetSafeHTMLString(string str)
{
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);
return str;
}
asp.net 字符串过滤的更多相关文章
- PHP:第五章——字符串过滤函数
<?php header("Content-Type:text/html;charset=utf-8"); //字符串过滤函数: //1.n12br 在所有新行之前插入Htm ...
- ASP.NET中过滤HTML字符串的两个方法
先记下来,以作备用! /// <summary>去除HTML标记 /// /// </summary> /// <param name="Htmlstring& ...
- 在ASP.NET中过滤HTML字符串总结
先记下来,以作备用! /// <summary>去除HTML标记 /// /// </summary> /// <param name="Htmls ...
- asp.net正则表达式过滤标签和数据提取
无论什么语言,正则表达式的处理方法都是非常灵活.高效的,尤其是对某些字符串的抓取.过滤方面,更显其优势. 正则表达式的写法通常比较简单,几行短代码便能轻松完成看似很复杂的事情,更值得称赞的是,它的执行 ...
- ASP.NET脚本过滤-防止跨站脚本攻击[转]
ASP.Net 1.1后引入了对提交表单自动检查是否存在XSS(跨站脚本攻击)的能力.当用户试图用<xxxx>之类的输入影响页面返回结果的时候,ASP.Net的引擎会引发一个HttpReq ...
- ASP.NET脚本过滤-防止跨站脚本攻击(收集别人的)
ASP.Net 1.1后引入了对提交表单自动检查是否存在XSS(跨站脚本攻击)的能力.当用户试图用<xxxx>之类的输入影响页面返回结果的时候,ASP.Net的引擎会引发一个 HttpRe ...
- Rails关闭html_safe字符串过滤
在某些情况下我希望html的文本中包含一些换行,因为html5产生换行的方法是插入 <br />所以我可以这么写: text = "hello world!<br /> ...
- java 性能优化 字符串过滤实战
转自[http://www.apkbus.com/blog-822717-78335.html] 如有不妥联系删除!! ★一个简单的需求 首先描述一下需求:给定一个 String 对象,过滤掉除了数 ...
- Asp.Net正则过滤一个div
Asp.Net过滤一对标签,例如div.ul.p.li.span等 html = “html page”; Regex regPage = new Regex(@"(?is)<div\ ...
随机推荐
- 在安卓JNI/NDK中使用C++11
在VS下编写的程序移植到Eclipse下,出现了问题: this file requires compiler and library support for iso C++ 2011 standar ...
- Android使用NDK---函数参数传递-基本类型和数组
参考链接:http://www.cnblogs.com/luxiaofeng54/archive/2011/08/19/2145486.html 数据传输可分为 基本数据类型传输 和 引用数据类型的传 ...
- Swift Pointer 使用指南
Overview C Syntax Swift Syntax Note const Type * UnsafePointer<Type> 指针可变,指针指向的内存值不可变. Type * ...
- tomcat ider配置
xml文件配置: <servlet> <servlet-name>test1</servlet-name>//设定java文件链接的锚点 <servlet-c ...
- canvas画弧线
arc(x, y, radius, startRad, endRad, [anticlockwise]) 在Canvas画布上绘制以坐标点(x,y)为圆心.半么为radius的圆上的一段弧线.这段弧线 ...
- wx小程序开发 1:小程序代码构成
官网学习地址:https://developers.weixin.qq.com/miniprogram/dev/quickstart/basic/introduction.html 1: 2:待续.. ...
- Android 性能测试初探(四)
书接上文 Android 性能测试初探(三) 自从 cpu及内存后,GPU 这个词对于 PC 性能测试者也不陌生了,什么 3Dmax,安兔兔之类的第三方软件让 GPU 在移动端性能测试领域都知晓,但对 ...
- [luogu2576 SCOI2010] 幸运数字 (容斥原理)
传送门 Description 在中国,很多人都把6和8视为是幸运数字!lxhgww也这样认为,于是他定义自己的"幸运号码"是十进制表示中只包含数字6和8的那些号码,比如68,66 ...
- java实现根据高德地图API接口进行地址位置解析,将地址转化为经纬度
原创文章,转载请注明,欢迎评论和更改. 1,所需额外ar包,import net.sf.json.JSONObject; 2,完整源代码代码 package com.travel.util; impo ...
- UOJ #214 合唱队形 (概率期望计数、DP、Min-Max容斥)
9个月的心头大恨终于切掉了!!!! 非常好的一道题,不知为何uoj上被点了70个差评. 题目链接: http://uoj.ac/problem/214 题目大意: 请自行阅读. 题解: 官方题解讲得相 ...