提取HTML代碼中文字的C#函數(shù)
更新時間:2007年03月16日 00:00:00 作者:
/// <summary>
/// 去除HTML標記
/// </summary>
/// <param name="strHtml">包括HTML的源碼 </param>
/// <returns>已經(jīng)去除后的文字</returns>
public static string StripHTML(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 [] aryRep = {
"",
"",
"",
"\"",
"&",
"<",
">",
" ",
"\xa1",//chr(161),
"\xa2",//chr(162),
"\xa3",//chr(163),
"\xa9",//chr(169),
"",
"\r\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,aryRep[i]);
}
strOutput.Replace("<","");
strOutput.Replace(">","");
strOutput.Replace("\r\n","");
return strOutput;
}
/// 去除HTML標記
/// </summary>
/// <param name="strHtml">包括HTML的源碼 </param>
/// <returns>已經(jīng)去除后的文字</returns>
public static string StripHTML(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 [] aryRep = {
"",
"",
"",
"\"",
"&",
"<",
">",
" ",
"\xa1",//chr(161),
"\xa2",//chr(162),
"\xa3",//chr(163),
"\xa9",//chr(169),
"",
"\r\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,aryRep[i]);
}
strOutput.Replace("<","");
strOutput.Replace(">","");
strOutput.Replace("\r\n","");
return strOutput;
}
相關(guān)文章
Graphics.DrawImage繪制的圖像變大的原因分析及解決
這篇文章主要介紹了Graphics.DrawImage繪制的圖像變大的原因分析及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
使用mutex實現(xiàn)應(yīng)用程序單實例運行代碼分享
本文主要介紹了使用Mutex實現(xiàn)應(yīng)用程序單實例運行的方法,實現(xiàn)原理是在程序啟動時,請求一個互斥體,如果能獲取對指定互斥的訪問權(quán),就繼續(xù)運行程序,否則就退出程序2014-01-01
C#連接Oracle數(shù)據(jù)庫的多種方法總結(jié)
最近小項目當(dāng)中要使用C#來連接Oracle數(shù)據(jù)庫來完成系統(tǒng)的操作,這篇文章主要給大家介紹了關(guān)于C#連接Oracle數(shù)據(jù)庫的多種方法,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2023-04-04

