Asp.Net 清除Html标签

/// <summary>
/// 清除Html标签
/// </summary>
/// <param name="content"></param>
/// <returns></returns>
protected string ClearHtmlTag(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, @"&(ldquo);", "“", RegexOptions.IgnoreCase);
htmlString = Regex.Replace(htmlString, @"&(rdquo);", "”", 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;
}

posted @ 2014-07-04 08:34  Xinney  阅读(274)  评论(0编辑  收藏  举报