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;
}
}
}
查看全文
相关阅读:
ubuntu 搭建 php 环境
【转】送给和我一样曾经浮躁过的PHPer程序猿,希望有帮助
thinkphp iis下去掉index.php
windows定时执行PHP的技巧
js 生成随机数字的方法
Linux下crontab命令的用法
收藏下(设为收藏,设为首页)
C#扩展方法的理解
Win7 访问共享时输入正确密码仍然提示密码错误
SQL Server 获取插入记录后的自动编号ID
原文地址:https://www.cnblogs.com/lann/p/1603051.html
最新文章
sqlserver数据库出现置疑的解决办法
MFC、SDK和API有什么区别
VC++开发垃圾文件清理软件(上)
系统要关闭,可我程序还有事要处理?
SQL 实现定期备份数据库
Linux下Socket的简单使用及最简化封装
删除开机启动项
powershell初探(三)
powershell初探(一)
powershell初探(六)
热门文章
powershell初探(四)
powershell初探(二)
powershell初探(五)
F1到F12键你会用多少?
双绞线网线线序解析
HTML元素语义的分类
FireBug 控制台函数说明
专业播客网FLV视频节目下载软件
Firefox常用插件及扩展
KMPlayer实现双语显示的教学篇
Copyright © 2011-2022 走看看