ASP.NET特殊字符过滤函数
2009-08-19 15:56
736 查看
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
/// <summary>
/// 对字符串进行检查和替换其中的特殊字符
/// </summary>
/// <param name="strHtml"></param>
/// <returns></returns>
public static string HtmlToTxt(string strHtml)
{
string[] aryReg ={
@"<script[^>]*?>.*?</script>",
@"<(///s*)?!?((/w+:)?/w+)(/w+(/s*=?/s*(([""'])(|[^/7])*?/7|/w+)|.{0})|/s)*?(///s]//[""'tbnr]|[^/7])*?/7|/w+)|.{0})|/s)*?(///s*)?>",
@"([/r/n])[/s]+",
@"&(quot|#34);",
@"&(amp|#38);",
@"&(lt|#60);",
@"&(gt|#62);",
@"&(nbsp|#160);",
@"&(iexcl|#161);",
@"&(cent|#162);",
@"&(pound|#163);",
@"&(copy|#169);",
@"(/d+);",
@"-->",
@"<!--.*/n"
};
string newReg = aryReg[0];
string strOutput = strHtml;
for (int i = 0; i < aryReg.Length; i++)
{
Regex regex = new Regex(aryReg[i], RegexOptions.IgnoreCase);
strOutput = regex.Replace(strOutput, string.Empty);
}
strOutput.Replace("<", "");
strOutput.Replace(">", "");
strOutput.Replace("/r/n", "");
return strOutput;
}
=================另外两个函数=====================
/// <summary>
/// 替换html中的特殊字符
/// </summary>
/// <param name="theString">需要进行替换的文本。</param>
/// <returns>替换完的文本。</returns>
public string HtmlEncode(string theString)
{
theString = theString.Replace(">", ">");
theString = theString.Replace("<", "<");
theString = theString.Replace(" ", " ");
theString = theString.Replace(" ", " ");
theString = theString.Replace("/"", """);
theString = theString.Replace("/'", "'");
theString = theString.Replace("/n", "<br/> ");
return theString;
}
/// <summary>
/// 恢复html中的特殊字符
/// </summary>
/// <param name="theString">需要恢复的文本。</param>
/// <returns>恢复好的文本。</returns>
public string HtmlDiscode(string theString)
{
theString = theString.Replace(">", ">");
theString = theString.Replace("<", "<");
theString = theString.Replace(" ", " ");
theString = theString.Replace(" ", " ");
theString = theString.Replace(""", "/"");
theString = theString.Replace("'", "/'");
theString = theString.Replace("<br/> ", "/n");
return theString;
}
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
/// <summary>
/// 对字符串进行检查和替换其中的特殊字符
/// </summary>
/// <param name="strHtml"></param>
/// <returns></returns>
public static string HtmlToTxt(string strHtml)
{
string[] aryReg ={
@"<script[^>]*?>.*?</script>",
@"<(///s*)?!?((/w+:)?/w+)(/w+(/s*=?/s*(([""'])(|[^/7])*?/7|/w+)|.{0})|/s)*?(///s]//[""'tbnr]|[^/7])*?/7|/w+)|.{0})|/s)*?(///s*)?>",
@"([/r/n])[/s]+",
@"&(quot|#34);",
@"&(amp|#38);",
@"&(lt|#60);",
@"&(gt|#62);",
@"&(nbsp|#160);",
@"&(iexcl|#161);",
@"&(cent|#162);",
@"&(pound|#163);",
@"&(copy|#169);",
@"(/d+);",
@"-->",
@"<!--.*/n"
};
string newReg = aryReg[0];
string strOutput = strHtml;
for (int i = 0; i < aryReg.Length; i++)
{
Regex regex = new Regex(aryReg[i], RegexOptions.IgnoreCase);
strOutput = regex.Replace(strOutput, string.Empty);
}
strOutput.Replace("<", "");
strOutput.Replace(">", "");
strOutput.Replace("/r/n", "");
return strOutput;
}
=================另外两个函数=====================
/// <summary>
/// 替换html中的特殊字符
/// </summary>
/// <param name="theString">需要进行替换的文本。</param>
/// <returns>替换完的文本。</returns>
public string HtmlEncode(string theString)
{
theString = theString.Replace(">", ">");
theString = theString.Replace("<", "<");
theString = theString.Replace(" ", " ");
theString = theString.Replace(" ", " ");
theString = theString.Replace("/"", """);
theString = theString.Replace("/'", "'");
theString = theString.Replace("/n", "<br/> ");
return theString;
}
/// <summary>
/// 恢复html中的特殊字符
/// </summary>
/// <param name="theString">需要恢复的文本。</param>
/// <returns>恢复好的文本。</returns>
public string HtmlDiscode(string theString)
{
theString = theString.Replace(">", ">");
theString = theString.Replace("<", "<");
theString = theString.Replace(" ", " ");
theString = theString.Replace(" ", " ");
theString = theString.Replace(""", "/"");
theString = theString.Replace("'", "/'");
theString = theString.Replace("<br/> ", "/n");
return theString;
}
相关文章推荐
- 一个过滤特殊字符的函数ASP.NET (C#)
- 过滤特殊字符的函数ASP.NET (C#)
- 一个过滤特殊字符的函数ASP.NET (C#)
- ASP几个函数(返回汉字首字拼音/过滤HTML字符/检查组件是否已经安装/过滤特殊字符)
- C#(ASP.NET)正则表达式 过滤危险字符函数代码 防SQL注入 很全面的SQL关键字过滤
- ASP.NET (C#) 过滤特殊字符(摘)
- ASP.NET 登陆界面中过滤特殊字符(使用c#)
- Asp过滤某些特殊字符的函数
- asp.net webapi 序列化为xml 时实体属性增加<![CDATA[]]>防止特殊字符
- asp.net 顯示UniCode特殊字符
- Webservice asp.net 请求 传递参数时 对特殊字符的验证
- asp 过滤非法字符函数
- ASP特殊字符过滤
- .net 字符串过滤标记(包括HTML,脚本,数据库关键字,特殊字符的源码)
- asp.net 特殊字符处理
- asp.net替换和恢复html特殊字符
- asp.net过滤HTML标签的几个函数
- asp.net 特殊(法文)字符的问题(é)