首页  ·  知识 ·  云计算
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
    >>频道首页  >>网站首页   纠错  >>投诉
版权声明:CIO之家尊重行业规范,每篇文章都注明有明确的作者和来源;CIO之家的原创文章,请转载时务必注明文章作者和来源;
延伸阅读
也许感兴趣的
我们推荐的
主题最新
看看其它的