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;
}
}
}
查看全文
相关阅读:
关于接口是值类型还是引用类型的猜测
絮语工作四年的碎碎念
烧钱游戏加入创业公司的一些感想
关于C#调用非托管动态库方式的性能疑问
couchbase作为分布式session容器时的注意事项
poj3624
明天的下载链接
poj 1502
poj1459 多源多汇最大流
poj 3041
原文地址:https://www.cnblogs.com/lann/p/1603051.html
最新文章
关于C语言中的无符号数和有符号数
Lighttpd1.4.20源码分析之fdevent系统(1)fdevents结构体和fdevent系统对外接口
[转载]一封尘封了八年的信
Lighttpd1.4.20源码分析之fdevent系统(2)初始化
Lighttpd1.4.20源码分析之array.c(h) 通用数组
Lighttpd1.4.20源码分析之插件系统(2)插件的加载和初始化
lighttpd1.4.20源码分析 工作模型
Lighttpd1.4.20源码分析之buffer.c(h)字符串内存管理
作业三
做汉堡
热门文章
作业2 结对 四则运算
四则运算
QT(mingw) 编译 boost
使用VS2015调试Qt5.9.5源码
qt5.9.0 msvc2015优雅的崩溃:dumpfile
k线、指标绘制
C++ 资源大全中文版
k线生成模块
期货、期权tick数据接收
C++_运算符重载 总结
Copyright © 2011-2022 走看看