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數據備份和還原
收藏 將此文推薦給朋友
分享到:
10个数复式三中三多少组公式