ASP.NET特殊字符串替换函数 - .net语言 -

ASP.NET特殊字符串替换函数

时间:2010-01-30 10:11:56   来源:   评论:加载中...   点击:加载中...
usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.Text.RegularExpressions;///<summary>///对字符串进行检查和...

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Text;

usingSystem.Text.RegularExpressions;

///<summary>

///对字符串进行检查和替换其中的特殊字符

///</summary>

///<paramname="strHtml"></param>

///<returns></returns>

publicstaticstringHtmlToTxt(stringstrHtml)

{

string[]aryReg={

@"<script[^>]*?>.*?</script>",

@"<(/s*)?!?((w+:)?w+)(w+(s*=?s*(([""'])([""'tbnr]|[^7])*?7|w+)|.{0})|s)*?(/s*)?>",

@"([rn])[s]+",

@"&(quot|#34);",

@"&(amp|#38);",

@"&(lt|#60);",

@"&(gt|#62);",

@"&(nbsp|#160);",

@"&(iexcl|#161);",

@"&(cent|#162);",

@"&(pound|#163);",

@"&(copy|#169);",

@"&#(d+);",

@"-->",

@"<!--.*n"

};

stringnewReg=aryReg[0];

stringstrOutput=strHtml;

for(inti=0;i<aryReg.Length;i++)

{

Regexregex=newRegex(aryReg[i],RegexOptions.IgnoreCase);

strOutput=regex.Replace(strOutput,string.Empty);

}

strOutput.Replace("<","");

strOutput.Replace(">","");

strOutput.Replace("rn","");

returnstrOutput;

}

=================另外两个函数=====================

///<summary>

///替换html中的特殊字符

///</summary>

///<paramname="theString">需要进行替换的文本。</param>

///<returns>替换完的文本。</returns>

publicstringHtmlEncode(stringtheString)

{

theString=theString.Replace(">","&gt;");

theString=theString.Replace("<","&lt;");

theString=theString.Replace("","&nbsp;");

theString=theString.Replace("","&nbsp;");

theString=theString.Replace(""","&quot;");

theString=theString.Replace("'","'");

theString=theString.Replace("n","<br/>");

returntheString;

}

///<summary>

///恢复html中的特殊字符

///</summary>

///<paramname="theString">需要恢复的文本。</param>

///<returns>恢复好的文本。</returns>

publicstringHtmlDiscode(stringtheString)

{

theString=theString.Replace("&gt;",">");

theString=theString.Replace("&lt;","<");

theString=theString.Replace("&nbsp;","");

theString=theString.Replace("&nbsp;","");

theString=theString.Replace("&quot;",""");

theString=theString.Replace("'","'");

theString=theString.Replace("<br/>","n");

returntheString;

}

 



相关热词搜索:

 
上一篇:利用WebClient和WebRequest类获得网页源代码
下一篇:.Net下的SQL Server数据备份和还原
收藏 将此文推荐给朋友
分享到: