ASP.NET過(guò)濾HTML字符串方法總結(jié)
更新時(shí)間:2014年08月06日 11:26:32 投稿:shichen2014
這篇文章主要介紹了ASP.NET過(guò)濾HTML字符串方法總結(jié),需要的朋友可以參考下
本文實(shí)例講述了ASP.NET過(guò)濾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)簽過(guò)濾,如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; }
您可能感興趣的文章:
- asp.NET 臟字過(guò)濾算法 修改版
- asp.NET 臟字過(guò)濾算法
- ASP.NET過(guò)濾器的應(yīng)用方法介紹
- asp.net 過(guò)濾圖片標(biāo)簽的正則
- ASP.NET環(huán)境下為網(wǎng)站增加IP過(guò)濾功能
- ASP.NET過(guò)濾HTML標(biāo)簽只保留換行與空格的方法
- ASP.NET中的URL過(guò)濾實(shí)現(xiàn)代碼
- asp.net 臟字典過(guò)濾問(wèn)題 用正則表達(dá)式來(lái)過(guò)濾臟數(shù)據(jù)
- Asp.net之?dāng)?shù)據(jù)過(guò)濾淺析
- ASP.NET過(guò)濾類(lèi)SqlFilter,防止SQL注入
- 高效.NET臟字過(guò)濾算法與應(yīng)用實(shí)例
相關(guān)文章
asp.net中頁(yè)面顯示當(dāng)前系統(tǒng)時(shí)間附圖
asp.net如何實(shí)現(xiàn)在頁(yè)面顯示當(dāng)前系統(tǒng)時(shí)間,本文有個(gè)不錯(cuò)的方法,大家可以嘗試操作下,在文章末尾有截圖2013-12-12Asp.Net類(lèi)庫(kù)中發(fā)送電子郵件的代碼
發(fā)送電子郵件是許多需要用戶注冊(cè)的網(wǎng)站的通用功能,通過(guò)正則表達(dá)式我們可以過(guò)濾掉不符合電子郵件格式的輸入,但是仍沒(méi)有辦法確保用戶填寫(xiě)的電子郵件地址一定是他本人真實(shí)有效的電子郵件地址2008-09-09asp.net core webapi文件上傳功能的實(shí)現(xiàn)
這篇文章主要介紹了asp.net core webapi文件上傳功能的實(shí)現(xiàn),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12.NET使用結(jié)構(gòu)體替代類(lèi)提升性能優(yōu)化的技巧
這篇文章主要介紹了.NET使用結(jié)構(gòu)體替代類(lèi)提升性能優(yōu)化的技巧,使用結(jié)構(gòu)體替代類(lèi)有什么好處呢?在什么樣的場(chǎng)景需要使用結(jié)構(gòu)體來(lái)替代類(lèi)呢?今天的文章為大家一一解答,需要的朋友可以參考下2022-05-05Asp.Net平臺(tái)下的圖片在線裁剪功能的實(shí)現(xiàn)代碼(源碼打包)
最近項(xiàng)目中有個(gè)圖片在線裁剪功能,本人查找資料,方法如下:前臺(tái)展現(xiàn)用jquery.Jcrop實(shí)現(xiàn),后臺(tái)使用 System.Drawing.Image類(lèi)來(lái)進(jìn)行裁剪2011-10-10