zoukankan
html css js c++ java
ASP.NET字符串过滤(转)
Code
using
System;
using
System.Text.RegularExpressions;
using
System.Web;
/**/
///
<summary>
///
StringOut 字符串输出的时候过滤数据
///
</summary>
public
class
MyDoString
{
public
MyDoString()
{
//
//
TODO: 在此处添加构造函数逻辑
//
}
/**/
///
<summary>
///
过滤标记
///
</summary>
///
<param name="NoHTML">
包括HTML,脚本,数据库关键字,特殊字符的源码
</param>
///
<returns>
已经去除标记后的文字
</returns>
public
static
string
NoHTML(
string
Htmlstring)
{
if
(Htmlstring
==
null
)
{
return
""
;
}
else
{
//
删 除脚本
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
=
Regex.Replace(Htmlstring,
"
xp_cmdshell
"
,
""
, RegexOptions.IgnoreCase);
//
删 除与数据库相关的词
//
Htmlstring = Regex.Replace(Htmlstring, "select", "", RegexOptions.IgnoreCase);
//
Htmlstring = Regex.Replace(Htmlstring, "insert", "", RegexOptions.IgnoreCase);
Htmlstring
=
Regex.Replace(Htmlstring,
"
delete from
"
,
""
, RegexOptions.IgnoreCase);
//
Htmlstring = Regex.Replace(Htmlstring, "count''", "", RegexOptions.IgnoreCase);
Htmlstring
=
Regex.Replace(Htmlstring,
"
drop table
"
,
""
, RegexOptions.IgnoreCase);
Htmlstring
=
Regex.Replace(Htmlstring,
"
truncate
"
,
""
, RegexOptions.IgnoreCase);
Htmlstring
=
Regex.Replace(Htmlstring,
"
asc
"
,
""
, RegexOptions.IgnoreCase);
//
Htmlstring = Regex.Replace(Htmlstring, "mid", "", RegexOptions.IgnoreCase);
//
Htmlstring = Regex.Replace(Htmlstring, "char", "", RegexOptions.IgnoreCase);
Htmlstring
=
Regex.Replace(Htmlstring,
"
xp_cmdshell
"
,
""
, RegexOptions.IgnoreCase);
Htmlstring
=
Regex.Replace(Htmlstring,
"
exec master
"
,
""
, RegexOptions.IgnoreCase);
Htmlstring
=
Regex.Replace(Htmlstring,
"
net localgroup administrators
"
,
""
, RegexOptions.IgnoreCase);
//
Htmlstring = Regex.Replace(Htmlstring, "and", "", RegexOptions.IgnoreCase);
Htmlstring
=
Regex.Replace(Htmlstring,
"
net user
"
,
""
, RegexOptions.IgnoreCase);
//
Htmlstring = Regex.Replace(Htmlstring, "or", "", RegexOptions.IgnoreCase);
//
Htmlstring = Regex.Replace(Htmlstring, "net", "", RegexOptions.IgnoreCase);
//
Htmlstring = Regex.Replace(Htmlstring,"*", "", RegexOptions.IgnoreCase);
//
Htmlstring = Regex.Replace(Htmlstring,"-", "", RegexOptions.IgnoreCase);
//
Htmlstring = Regex.Replace(Htmlstring, "delete", "", RegexOptions.IgnoreCase);
//
Htmlstring = Regex.Replace(Htmlstring, "drop", "", RegexOptions.IgnoreCase);
//
Htmlstring = Regex.Replace(Htmlstring, "script", "", RegexOptions.IgnoreCase);
//
特殊的字符
//
Htmlstring = Htmlstring.Replace("<", "");
//
Htmlstring = Htmlstring.Replace(">", "");
Htmlstring
=
Htmlstring.Replace(
"
*
"
,
""
);
//
Htmlstring = Htmlstring.Replace("-", "");
//
Htmlstring = Htmlstring.Replace("?", "");
//
Htmlstring = Htmlstring.Replace(",", "");
//
Htmlstring = Htmlstring.Replace("/", "");
Htmlstring
=
Htmlstring.Replace(
"
;
"
,
""
);
Htmlstring
=
Htmlstring.Replace(
"
*/
"
,
""
);
Htmlstring
=
Htmlstring.Replace(
"
\r\n
"
,
""
);
Htmlstring
=
HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();
return
Htmlstring;
}
}
}
查看全文
相关阅读:
Js特效之放大镜(淘宝商品展示)
关于使用JavaScript实现图片点击切换(附带改变导航图片 方案二)
关于使用JavaScript实现图片点击切换(附带改变导航图片 方案一)
JavaScript常用的方法和函数(setInterval和setTimeout)
When to use dequeueReusableCellWithIdentifier vs dequeueReusableCellWithIdentifier: forIndexPath
iOS 7.1 安装 企业应用 提示 无法下载应用程序
Git忽略规则及.gitignore规则不生效的解决办法
Core Animation 学习
AutoLayout那些坑
欲善其事必先利其器---Xcode插件
原文地址:https://www.cnblogs.com/lann/p/1603051.html
最新文章
mysql 导入命令source
8.使用for循环和while循环遍历文件
asp net core 跨平台初体验
获取图片的主色调
【C语言】指针
Visual Studio 常用快捷键 (二)
Visual Studio 常用快捷键(一)
js分页
Oracle 游标使用全解
Oracle SQL语句优化34条
热门文章
oracle常用SQL语句(汇总版)
VS2008、 VS2010 、 VS2012、 VS2013 都能用的快捷键
写C#代码时用到的中文简体字 、繁体字 对应的转化 (收藏吧)
C# HashTable 使用用法详解
flex中的图片垂直居中
vue--环境搭建及目录解析
webstorm安装与本地激活
倒计时(距离活动结束还有X天X小时X分X秒)
H5游戏见缝插针开发
hammer的初始化及移动端各种滑动
Copyright © 2011-2022 走看看