zoukankan      html  css  js  c++  java
  • c# 过滤html

      public static string StripHTML(string html)
            {
                try
                {
                    System.Text.RegularExpressions.Regex regex1 = new System.Text.RegularExpressions.Regex(@"<script[sS]+</script *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                    System.Text.RegularExpressions.Regex regex2 = new System.Text.RegularExpressions.Regex(@" href *= *[sS]*script *:", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                    System.Text.RegularExpressions.Regex regex3 = new System.Text.RegularExpressions.Regex(@" on[sS]*=", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                    System.Text.RegularExpressions.Regex regex4 = new System.Text.RegularExpressions.Regex(@"<iframe[sS]+</iframe *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                    System.Text.RegularExpressions.Regex regex5 = new System.Text.RegularExpressions.Regex(@"<frameset[sS]+</frameset *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                    System.Text.RegularExpressions.Regex regex6 = new System.Text.RegularExpressions.Regex(@"<img[^>]+>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                    System.Text.RegularExpressions.Regex regex7 = new System.Text.RegularExpressions.Regex(@"</p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                    System.Text.RegularExpressions.Regex regex8 = new System.Text.RegularExpressions.Regex(@"<p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
    
                    html = regex1.Replace(html, ""); //过滤<script></script>标记
                    html = regex2.Replace(html, ""); //过滤href=javascript: (<A>) 属性
                    html = regex3.Replace(html, " _disibledevent="); //过滤其它控件的on事件
                    html = regex4.Replace(html, ""); //过滤iframe
                    html = regex5.Replace(html, ""); //过滤frameset
                    html = regex6.Replace(html, ""); //过滤frameset
                    html = regex7.Replace(html, ""); //过滤frameset
                    html = regex8.Replace(html, ""); //过滤frameset
                    html = html.Replace(" ", "");
                    html = html.Replace("</strong>", "");
                    html = html.Replace("<strong>", "");
                    html = html.Replace("
    ", "");
                    html = html.Replace("
    ", "");
                    html = html.Replace("'", "");
                    html = html.Replace(""", "");
                    html = html.Replace("	", "");
                    return html;
                }
                catch
                {
                    //MessageBox.Show("Error");
                    return html;
                }
            }
  • 相关阅读:
    LDAP入门
    Java程序员书籍推荐
    docker registry镜像容器时区时间同步
    微服务架构与实践及云原生等相关概念
    ts基础
    内网穿透连接内网下的"我的世界"服务端
    [量子互联] 内网穿透远程连接Linux的SSH
    树莓派开启SSH的N种方法
    [量子互联] 群晖NAS的远程映射配置
    [量子互联] 群晖NAS的qBittorrent端口映射
  • 原文地址:https://www.cnblogs.com/feizianquan/p/9720751.html
Copyright © 2011-2022 走看看