过滤HTML 脚本 样式 避免样式冲突

 public static string ToSafeHtmlString(string content)
{
if (content == null)
{
return string.Empty;
}
if (string.IsNullOrWhiteSpace(content))
{
return content;
}
content = Regex.Replace(content, @"<script[^<>]*?>[\s\S]*?</script>", String.Empty, RegexOptions.IgnoreCase);
content = Regex.Replace(content, @"<([\w]+?[^<>]*?) (class|style)=""[^""]*?""([^<>]*?)>", "<$1$3>", RegexOptions.IgnoreCase);
content = Regex.Replace(content, @"<([\w]+?[^<>]*?) (class|style)='[^']*?'([^<>]*?)>", "<$1$3>", RegexOptions.IgnoreCase);
content = Regex.Replace(content, @"<([\w]+?[^<>]*?) (class|style)=[^<>]*?( [^<>]*?|)>", "<$1$3>", RegexOptions.IgnoreCase);
content = Regex.Replace(content, @"<frameset[^<>]*?>[\s\S]*?</frameset>", String.Empty, RegexOptions.IgnoreCase);
content = Regex.Replace(content, @"<frameset[^<>]*?>", String.Empty, RegexOptions.IgnoreCase);
content = Regex.Replace(content, @"<iframe[^<>]*?>[\s\S]*?</iframe>", String.Empty, RegexOptions.IgnoreCase);
content = Regex.Replace(content, @"<iframe[^<>]*?>", String.Empty, RegexOptions.IgnoreCase);
content = Regex.Replace(content, @"<script[^<>]*?>", String.Empty, RegexOptions.IgnoreCase);
return content;
}



posted @ 2012-03-22 09:19  Rhythmk  阅读(484)  评论(0编辑  收藏  举报
Rhythmk 个人笔记