我收藏的特殊字符过滤函数
来源:互联网 发布:win电脑桌面整理软件 编辑:程序博客网 时间:2024/06/05 09:29
希望尽量不是直接要实例就去用,而不关心他本来的意义和实现思路等。加油!
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;
}
- 我收藏的特殊字符过滤函数
- 我收藏的特殊字符过滤函数
- 过滤特殊字符函数
- Asp过滤某些特殊字符的函数
- PHP过滤所有特殊字符的函数
- php 过滤特殊字符函数
- 我用的过滤字符函数
- 很简单的过滤特殊字符的函数
- 过滤特殊字符的函数ASP.NET (C#)
- 一个过滤特殊字符的函数ASP.NET (C#)
- php 超级好用的特殊字符过滤函数
- 过滤特殊字符的方法
- js特殊字符的过滤
- ASP.NET特殊字符过滤函数
- php过滤替换特殊字符 函数
- 一个过滤特殊字符的JS
- 一个过滤特殊字符的JS!
- javascript中对特殊字符的过滤
- LaTeX installation under linux
- Ext的日期格式说明
- 进入一门语言的途径
- pcitools in OpenSolaris, a good pci debug tool
- 8.21
- 我收藏的特殊字符过滤函数
- 在asp.net 2.0中结合母板页使用meta标签(扩展@Page指令)
- 一个结构体的内存布局
- 谷歌正式推出中国版谷歌软件精选
- execute、executeQuery和executeUpdate之间的区别
- 怎样导入两表之间的差异列
- server.xml
- 设计缺陷暴露引发互联网“地震”
- 盖茨:软件边界不断改变令人兴奋