zoukankan      html  css  js  c++  java
  • 过滤HTML代码

      public static string FilterHtml(string string_include_html)
            {
                string[] HtmlRegexArr ={ 
                                           #region Html 正则数组
                                @"<script[^>]*?>.*?</script>",  
                                 @"<(/s*)?!?((w+:)?w+)(w+(s*=?s*(([""'])(\[""'tbnr]|[^7])*?7|w+)|.{0})|s)*?(/s*)?>", 
                                 @"([
    ])[s]+", 
                                 @"&(quot|#34);", 
                                 @"&(amp|#38);", 
                                 @"&(lt|#60);", 
                                 @"&(gt|#62);", 
                                 @"&(nbsp|#160);", 
                                 @"&(iexcl|#161);", 
                                 @"&(cent|#162);", 
                                 @"&(pound|#163);", 
                                 @"&(copy|#169);", 
                                 @"&#(d+);", 
                                 @"-->", 
                                 @"<!--.*
    "  
                                            #endregion 
                                       };
                string[] HtmlReplaceArr = { 
                                           #region 替换Html字符
                                 "", 
                                 "", 
                                 "", 
                                 """, 
                                 "&", 
                                 "<", 
                                 ">", 
                                 " ", 
                                 "xa1", 
                                 "xa2", 
                                 "xa3", 
                                 "xa9", 
                                 "", 
                                 "
    ", 
                                 ""  
                                #endregion
                                          };
                string string_no_html = null;
                for (int i = 0; i < HtmlRegexArr.Length; i++)
                {
                    System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(HtmlRegexArr[i], System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                    string_no_html = regex.Replace(string_include_html, HtmlReplaceArr[i]);
                }
                string_no_html.Replace("<", "");
                string_no_html.Replace(">", "");
                string_no_html.Replace("
    ", "");
                return string_no_html;
            }

     以上来至网络,但个人认为还是不行。故有以下自己写的:

            /// <summary> 
            /// 将Html标签转化为空 
            /// </summary> 
            /// <param name="strHtml">待转化的字符串</param> 
            /// <returns>经过转化的字符串</returns> 
            public static string GetStringNoHtml(string string_include_html)
            {
                if (String.IsNullOrEmpty(string_include_html))
                {
                    return "";
                }
                else
                {
                    string_include_html = string_include_html.Replace("<BR>", "
    ").Replace("<br>", "
    ");
                    //第一种
                    string string_no_html = System.Text.RegularExpressions.Regex.Replace(string_include_html, @"(<script[^>]*?>.*?</script>)|(<(.[^>]*)>)", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                    return string_no_html.Replace("&nbsp;", " ");
                    //第二种
                    //return System.Text.RegularExpressions.Regex.Replace(string_include_html, @"(<script[^>]*?>.*?</script>)|(<(.[^>]*)>)|(&nbsp;)", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                }
            }
     
     
     
     
     
    讓眾人的薪枝構起這團熱情的火焰
  • 相关阅读:
    深度学习 Deep Learning UFLDL 最新Tutorial 学习笔记 3:Vectorization
    关于gcc的一点小人性化提示
    python 命令行參数解析
    一起talk C栗子吧(第九回:C语言实例--最大公约数)
    小程序 通用请求
    小程序 上啦下拉刷新window配置
    微信小程序 功能函数 将对象的键添加到数组 (函数深入)
    微信小程序 功能函数 点击传参和页面
    微信小程序 功能函数 购物车商品删除
    微信 小程序组件 分页菜单带下划线焦点切换
  • 原文地址:https://www.cnblogs.com/valeb/p/3637143.html
Copyright © 2011-2022 走看看