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(">",">");
theString=theString.Replace("<","<");
theString=theString.Replace(""," ");
theString=theString.Replace(""," ");
theString=theString.Replace(""",""");
theString=theString.Replace("'","'");
theString=theString.Replace("n","<br/>");
returntheString;
}
///<summary>
///恢复html中的特殊字符
///</summary>
///<paramname="theString">需要恢复的文本。</param>
///<returns>恢复好的文本。</returns>
publicstringHtmlDiscode(stringtheString)
{
theString=theString.Replace(">",">");
theString=theString.Replace("<","<");
theString=theString.Replace(" ","");
theString=theString.Replace(" ","");
theString=theString.Replace(""",""");
theString=theString.Replace("'","'");
theString=theString.Replace("<br/>","n");
returntheString;
}
|
||
相关热词搜索: |
||
收藏到网摘: | ||
|