首页  ·  知识 ·  云计算
ASP.NET过滤所有HTML标签几种方法
网友  其它|  综合  编辑:德仔   图片来源:网络
public static string nohtml(string htmlstring) { //删除脚本 nbsp
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, @"([rn])[s]+", "",
  10:            regexoptions.ignorecase);
  11:          htmlstring = regex.replace(htmlstring, @"-->", "", regexoptions.ignorecase);
  12:          htmlstring = regex.replace(htmlstring, @"<!--.*", "", regexoptions.ignorecase);
  13:          htmlstring = regex.replace(htmlstring, @"&(quot|#34);", """,
  1            regexoptions.ignorecase);
  1          htmlstring = regex.replace(htmlstring, @"&(amp|#38);", "&",
  16:            regexoptions.ignorecase);
  17:          htmlstring = regex.replace(htmlstring, @"&(lt|#60);", "<",
  18:            regexoptions.ignorecase);
  19:          htmlstring = regex.replace(htmlstring, @"&(gt|#62);", ">",
  20:            regexoptions.ignorecase);
  21:          htmlstring = regex.replace(htmlstring, @"&(nbsp|#160);", "   ",
  22:            regexoptions.ignorecase);
  23:          htmlstring = regex.replace(htmlstring, @"&(iexcl|#161);", "xa1",
  2            regexoptions.ignorecase);
  2          htmlstring = regex.replace(htmlstring, @"&(cent|#162);", "xa2",
  26:            regexoptions.ignorecase);
  27:          htmlstring = regex.replace(htmlstring, @"&(pound|#163);", "xa3",
  28:            regexoptions.ignorecase);
  29:          htmlstring = regex.replace(htmlstring, @"&(copy|#169);", "xa9",
  30:            regexoptions.ignorecase);
  31:          htmlstring = regex.replace(htmlstring, @"&#(d+);", "",
  32:            regexoptions.ignorecase);
  33:  
  3          htmlstring.replace("<", "");
  3          htmlstring.replace(">", "");
  36:          htmlstring.replace("rn", "");
  37:          htmlstring = httpcontext.current.server.htmlencode(htmlstring).trim();
  38:  
  39:          return htmlstring;
  40:      }
 
 
  //方法二
 
  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, @"([rn])[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("rn", "");
  htmlstring = httpcontext.current.server.htmlencode(htmlstring).trim();
  return htmlstring;
  }
  //方法三
 
 
/// <summary>
/// 过滤html标签
/// </summary>
/// <param name="html"></param>
/// <returns></returns>
public static string removehtmltags(string html)
 {
       html = regex.replace(html, "<script[^>]*?>.*?</script>", "", regexoptions.ignorecase);
       html = regex.replace(html, "<[^>]*>", "", regexoptions.ignorecase);
       return html;
}
///方法四
str = system.text.regularexpressions.regex.replace(str,"<[^>]*?>", "");
string result = regex.replace(html,@"< */? *w+[^>]*>","");
更多详细内容请查看:http://www.111cn.net/net/net/34667.htm
本文作者:网友 来源:其它|
CIO之家 www.ciozj.com 微信公众号:imciow
   
免责声明:本站转载此文章旨在分享信息,不代表对其内容的完全认同。文章来源已尽可能注明,若涉及版权问题,请及时与我们联系,我们将积极配合处理。同时,我们无法对文章内容的真实性、准确性及完整性进行完全保证,对于因文章内容而产生的任何后果,本账号不承担法律责任。转载仅出于传播目的,读者应自行对内容进行核实与判断。请谨慎参考文章信息,一切责任由读者自行承担。
延伸阅读