欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP.NET过滤HTML字符串方法总结

程序员文章站 2024-02-22 08:15:34
本文实例讲述了asp.net过滤html字符串的方法,供大家参考使用,具体代码如下: /// 去除html标记 ///...

本文实例讲述了asp.net过滤html字符串的方法,供大家参考使用,具体代码如下:

///  <summary>去除html标记 
///     
///  </summary>   
///  <param name="htmlstring">包括html的源码</param>   
///  <returns>已经去除后的文字</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标签,但把危险的html标签过滤,如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; 
}