最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

ASP.NET過濾HTML標(biāo)簽只保留換行與空格的方法

 更新時間:2014年12月23日 11:00:51   投稿:shichen2014  
這篇文章主要介紹了ASP.NET過濾HTML標(biāo)簽只保留換行與空格的方法,包含網(wǎng)上常見的方法以及對此方法的改進(jìn),具有一定的參考借鑒價值,需要的朋友可以參考下

本文實(shí)例講述了ASP.NET過濾HTML標(biāo)簽只保留換行與空格的方法。分享給大家供大家參考。具體分析如下:

自己從網(wǎng)上找了一個過濾HTML標(biāo)簽的方法,我也不知道誰的才是原創(chuàng)的,反正很多都一樣。我把那方法復(fù)制下來,代碼如下:

復(fù)制代碼 代碼如下:
///   <summary>
///   去除HTML標(biāo)記
///   </summary>
///   <param name="NoHTML">包括HTML的源碼   </param>
///   <returns>已經(jīng)去除后的文字</returns>
public static string NoHTML(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;
}

以上代碼是從網(wǎng)上直接復(fù)制過來的,這個確實(shí)能過濾掉所有的HTML標(biāo)簽,但是這個不是我想要的,這個過濾得太干凈了,我如果用textarea輸入框的話,我是要保留空格跟換行的。

然后我就自己改了一下這個方法,textarea的換行是\n,所以我得把這些標(biāo)簽重新匹配替換成<br>,這樣的話從數(shù)據(jù)庫中讀取到頁面時,就能正確的換行了,把空格替換成HTML的空格符,大功告成。

復(fù)制代碼 代碼如下:
///   <summary>
///   去除HTML標(biāo)記(保留br跟\r\n)
///   </summary>
///   <param   name="NoHTML">包括HTML的源碼   </param>
///   <returns>已經(jīng)去除后的文字</returns>
public static string NewNoHTML(string Htmlstring)
{
    //Htmlstring.Replace("\\r\\n", "%r%n").Replace("<br>","%br%").Replace("<br/>","%br&%").Replace("\\n","%n");
    //刪除腳本
    Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "",
      RegexOptions.IgnoreCase);
    //刪除HTML
    Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "",
      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);
    Htmlstring = Regex.Replace(Htmlstring, @"((\r\n))", "<br>");
    Htmlstring = Regex.Replace(Htmlstring, @"(\r|\n)", "<br>");
    Htmlstring = Regex.Replace(Htmlstring, @"(\s)", "&nbsp;");
    return Htmlstring;
}

這個過濾可以用于讓用戶輸入發(fā)布內(nèi)容時的過濾。

希望本文所述對大家的asp.net程序設(shè)計有所幫助。

相關(guān)文章

最新評論

措勤县| 浏阳市| 田林县| 砚山县| 稷山县| 简阳市| 太白县| 当雄县| 镇沅| 奈曼旗| 安徽省| 桃源县| 莱西市| 喀喇沁旗| 句容市| 论坛| 封丘县| 镇原县| 沁源县| 全州县| 交口县| 同江市| 漳州市| 措美县| 客服| 娄烦县| 迁安市| 呼伦贝尔市| 黄浦区| 松阳县| 乌拉特后旗| 来安县| 泸溪县| 吉水县| 新余市| 湟中县| 泸水县| 青海省| 民勤县| 德庆县| 双城市|