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;
}
}
}
查看全文
相关阅读:
[HNOI2012]矿场搭建
舞蹈链
POJ Apocalypse Someday
扩展卢卡斯定理
矩阵求逆
RandomAccsiFile
1.单例设计模式
MySQL 7.多表操作
IO流之Properties(配置文件)
MySQL 6.子查询
原文地址:https://www.cnblogs.com/lann/p/1603051.html
最新文章
一 Storm 基础
六 连接模式 1) 连接简介
五 数据组织模式 4)全排序、混排。
五 数据组织模式 3) 分箱 & 代码
五 数据组织模式 2) 分区模式
五 数据组织模式 (重组数据) 1 分层结构模式
H5调试
野指针崩溃问题,可以这样尝试下
Xcode出现( linker command failed with exit code 1)错误总结
枚举类型
热门文章
iOS socket常用数据类型转换
横屏模式注意点
关于Image创建的内存管理
写给自己的小CASE
byte -> int
对象交互
POJ Largest Rectangle in a Histogram
高精模板
LOJ146DFS 序 3,树上差分 1
三元上升子序列
Copyright © 2011-2022 走看看