我收藏的特殊字符过滤函数
2008-10-19 16:07
633 查看
这个我这里自己一直用的几个函数,下面贴出来。
希望尽量不是直接要实例就去用,而不关心他本来的意义和实现思路等。加油!
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*(([""'])(//[""'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;
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*(([""'])(//[""'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;
}
相关文章推荐
- 我收藏的特殊字符过滤函数
- 我收藏的特殊字符过滤函数
- 函数stripslashes去除转义 shopnc 搜索框过滤特殊字符 输入单斜杆会自动转义
- 过滤特殊字符的函数ASP.NET (C#)
- 很简单的过滤特殊字符的函数
- php过滤替换特殊字符 函数
- Asp过滤某些特殊字符的函数
- 一个过滤特殊字符的函数ASP.NET (C#)
- PHP过滤所有特殊字符的函数
- javascript特殊字符过滤函数
- ASP几个函数(返回汉字首字拼音/过滤HTML字符/检查组件是否已经安装/过滤特殊字符)
- 一个过滤特殊字符的函数ASP.NET (C#)
- ASP.NET特殊字符过滤函数
- 常用特殊字符过滤
- (手机表情过滤) Emoji与unicode特殊字符的处理
- Java过滤特殊字符的正则表达式----转载
- java正则表达式 过滤特殊字符的正则表达式
- lua中字符串过滤表情及特殊字符