ASP.NET特殊字符串替换函数
2009-11-23 11:37
363 查看
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.NET特殊字符串替换函数
- Asp.Net替换字符串
- 字段名称Asp.Net中替换JSON中主键内容的函数(很简单,示例+说明)
- asp.net 数组中字符串替换的几种方式
- Asp.net 字符串操作基类(安全,替换,分解等)
- VB.NET 字符串搜索、比较、筛选与替换函数(转帖)
- asp.net 字符串替换、截取和从字符串中最后某个字符 开始截取
- Asp.net 字符串操作基类(安全,替换,分解等)
- ASP.NET通过自定义函数实现对字符串的大小写切换功能
- asp.net字符串分割函数使用方法分享
- asp.net字符串分割函数用法
- asp.net处理Json数据时单引号与特殊字符串的处理
- 过滤特殊字符的函数ASP.NET (C#)
- C#构造方法(函数) C#方法重载 C#字段和属性 MUI实现上拉加载和下拉刷新 SVN常用功能介绍(二) SVN常用功能介绍(一) ASP.NET常用内置对象之——Server sql server——子查询 C#接口 字符串的本质 AJAX原生JavaScript写法
- C# ASP.NET替换要转化为json的数据中的特殊符号
- Asp.Net中替换JSON中主键内容的函数(很简单,示例+说明)
- ASP FSO文件操作函数代码(复制文件、重命名文件、删除文件、替换字符串)
- asp.net 替换字符串,正则替换字符串,不区分大小写替换字符串,替换html标签,正则匹配
- Asp.net 字符串操作基类(安全,替换,分解等)
- asp.net 字符串替换、截取。