当前位置:编程学习 > C#/ASP.NET >>

ASP.NET过滤所有HTML标签几种方法

asp教程.net过滤所有html标签几种方法

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+[^>]*>","");


%>

补充:asp.net教程,.Net开发
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,