过滤html字符的方法

昨天在看公司网站的产品详细页面,发现只有一个公司的JS产品轮换特效不会出,找了半天,程序都是一样的,原因是什么呢?原来是公司的别名是xiandahg,里面含有and字符被过滤掉了。

public static string NoHTML(string Htmlstring) //去除HTML标记
{
//删除脚本
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 = Htmlstring.Replace("[\\s]{2,{", " ");
Htmlstring = Htmlstring.Replace("( <[b|B][r|R]/*>)+|( <[p|P](.|\\n)*?>)", "\n"); // <br>
Htmlstring = Htmlstring.Replace("(\\s*&[n|N][b|B][s|S][p|P];\\s*)+", " "); //&nbsp;
Htmlstring = Htmlstring.Replace(" <(.|\\n)*?>", string.Empty); //any other tags
//Htmlstring = Htmlstring.Replace(" ", "&nbsp;");
Htmlstring = Htmlstring.Replace("&nbsp;", "");
Htmlstring = Htmlstring.Replace("\r\n", "<br>");
Htmlstring = Htmlstring.Replace("=", "");
Htmlstring = Htmlstring.Replace("%", "");
Htmlstring = Htmlstring.Replace("‘", "");
Htmlstring = Htmlstring.Replace("select ", "");
Htmlstring = Htmlstring.Replace("insert ", "");
Htmlstring = Htmlstring.Replace("delete ", "");
Htmlstring = Htmlstring.Replace(" or", "");
Htmlstring = Htmlstring.Replace("exec", "");
Htmlstring = Htmlstring.Replace("--", "");
Htmlstring = Htmlstring.Replace(" and", "");
Htmlstring = Htmlstring.Replace("where", "");
Htmlstring = Htmlstring.Replace("update", "");
Htmlstring = Htmlstring.Replace("script", "");
Htmlstring = Htmlstring.Replace("iframe", "");
Htmlstring = Htmlstring.Replace("master", "");
Htmlstring = Htmlstring.Replace("exec", "");
Htmlstring = Htmlstring.Replace(" AND", "");
Htmlstring = Htmlstring.Replace(" And", "");
Htmlstring = Htmlstring.Replace(" AnD", "");
Htmlstring = Htmlstring.Replace(" ANd", "");
Htmlstring = Htmlstring.Replace(" aND", "");
Htmlstring = Htmlstring.Replace(" aNd", "");
Htmlstring = Htmlstring.Replace(" anD", "");

Htmlstring = Htmlstring.Replace("exists", "");
Htmlstring = Htmlstring.Replace("drop", "");
Htmlstring = Htmlstring.Replace("996008E", "");
Htmlstring = Htmlstring.Replace("ar(124)", "");
Htmlstring = Htmlstring.Replace("1and", "");
Htmlstring = Htmlstring.Replace("0and", "");
Htmlstring = Htmlstring.Replace("chr(", "");

Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();

return Htmlstring;
}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。