过滤掉html 标签

/// <summary>
/// 过滤掉html标签
/// </summary>
/// <param name="Htmlstring"></param>
/// <returns></returns>
public static string RemoveHTML(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 = Regex.Replace(htmlstring, @"<img[^>]*>;", "", RegexOptions.IgnoreCase);
htmlstring = htmlstring.Replace("<", "");
htmlstring = htmlstring.Replace(">", "");
htmlstring = htmlstring.Replace("\r\n", "");
htmlstring = HttpContext.Current.Server.HtmlEncode(htmlstring).Trim();
return htmlstring;
}

 

posted on 2015-05-06 15:03  雪原日暮  阅读(368)  评论(0编辑  收藏  举报