zoukankan      html  css  js  c++  java
  • 过滤HTML字符

    #region 删除所有HTML标签
            /// <summary>
            /// 删除所有HTML标签
            /// </summary>
            /// <param name="Htmlstring"></param>
            /// <returns></returns>
            public static string NoHTML(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, @"&#(\d+);", "", RegexOptions.IgnoreCase);

                Htmlstring.Replace("<", "");
                Htmlstring.Replace(">", "");
                Htmlstring.Replace("\r\n", "");
                Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();

                Htmlstring=ClearString.ClearRequest(Htmlstring);    //过滤关键字

                return Htmlstring;
            }
            #endregion

            #region   移除HTML标签
            ///   <summary>  
            ///   移除HTML标签  
            ///   </summary>  
            ///   <param   name="HTMLStr">HTMLStr</param>  
            public static string ParseTags(string HTMLStr)
            {
                HTMLStr = ClearString.ClearRequest(HTMLStr);    //过滤关键字
                return System.Text.RegularExpressions.Regex.Replace(HTMLStr, "<[^>]*>", "");
            }

            #endregion  

  • 相关阅读:
    沙尘天气,但还是要坚持锻炼
    为了欧冠,堕落两天
    NRF24L01测试板子完成了
    昨天参加ti的研讨会了
    自我安慰一下
    功率W与dBm的对照表及关系
    短时间提高英语口语方法
    看了一个星期的欧洲杯,熬阿
    最近比较忙,项目较累
    后台获取js赋给服务器控件的值
  • 原文地址:https://www.cnblogs.com/di305449473/p/1193301.html
Copyright © 2011-2022 走看看