ASP.NET過濾HTML字符串方法總結(jié)
本文實(shí)例講述了ASP.NET過濾HTML字符串的方法,供大家參考使用,具體代碼如下:
/// <summary>去除HTML標(biāo)記
///
/// </summary>
/// <param name="Htmlstring">包括HTML的源碼</param>
/// <returns>已經(jīng)去除后的文字</returns>
public static string GetNoHTMLString(string Htmlstring)
{
//刪除腳本
Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
//刪除HTML
Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
Htmlstring.Replace("<", "");
Htmlstring.Replace(">", "");
Htmlstring.Replace("\r\n", "");
Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();
return Htmlstring;
}
/// <summary>獲取顯示的字符串,可顯示HTML標(biāo)簽,但把危險(xiǎn)的HTML標(biāo)簽過濾,如iframe,script等。
///
/// </summary>
/// <param name="str">未處理的字符串</param>
/// <returns></returns>
public static string GetSafeHTMLString(string str)
{
str = Regex.Replace(str, @"<applet[^>]*?>.*?</applet>", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"<body[^>]*?>.*?</body>", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"<embed[^>]*?>.*?</embed>", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"<frame[^>]*?>.*?</frame>", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"<frameset[^>]*?>.*?</frameset>", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"<html[^>]*?>.*?</html>", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"<iframe[^>]*?>.*?</iframe>", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"<style[^>]*?>.*?</style>", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"<layer[^>]*?>.*?</layer>", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"<link[^>]*?>.*?</link>", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"<ilayer[^>]*?>.*?</ilayer>", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"<meta[^>]*?>.*?</meta>", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"<object[^>]*?>.*?</object>", "", RegexOptions.IgnoreCase);
return str;
}
相關(guān)文章
ASP.NET全棧開發(fā)教程之前后臺(tái)校驗(yàn)結(jié)合詳解
這篇文章主要給大家介紹了關(guān)于ASP.NET全棧開發(fā)教程之前后臺(tái)校驗(yàn)結(jié)合的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
asp.net gridview的Rowcommand命令中獲取行索引的方法總結(jié)
asp.net gridview的Rowcommand命令中獲取行索引的方法總結(jié),需要的朋友可以參考下。2010-05-05
vs2010出現(xiàn)error MSB8008的解決方法
這篇文章主要為大家詳細(xì)介紹了vs2010問題error MSB8008: 指定的平臺(tái)工具集(v110)未安裝或無效的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
.Net中Task Parallel Library的進(jìn)階用法
這篇文章介紹了.Net中Task Parallel Library的進(jìn)階用法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-10-10
Entity?Framework使用DataBase?First模式實(shí)現(xiàn)數(shù)據(jù)庫的增刪改查
本文詳細(xì)講解了Entity?Framework使用DataBase?First模式實(shí)現(xiàn)數(shù)據(jù)庫的增刪改查,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03
ASP.NET Core設(shè)置URLs的方法匯總(完美解決.NET 6項(xiàng)目局域網(wǎng)IP地址遠(yuǎn)程無法訪問的
近期在dotnet項(xiàng)目中遇到這樣的問題.net6 運(yùn)行以后無法通過局域網(wǎng)IP地址遠(yuǎn)程訪問,整理出解決問題的五種方式方法,感興趣的朋友一起看看吧2023-11-11
Asp.net實(shí)現(xiàn)手寫驗(yàn)證碼的操作代碼
這篇文章主要介紹了Asp.net實(shí)現(xiàn)手寫驗(yàn)證碼的操作代碼,首先我們來實(shí)現(xiàn)生成 生成驗(yàn)證碼的這一部分,這時(shí)候我們需要封裝一個(gè)類,里面有生成圖片和字符的方法,其實(shí)本質(zhì)上就是一個(gè)隨機(jī)數(shù),這個(gè)類卸載Model里面即可,具體操作代碼跟隨小編一起看看吧2024-05-05

