zoukankan      html  css  js  c++  java
  • Html标签替换(过滤掉html特殊符号)

    /// <summary>
            /// 替换标签,把&nbsp;&lt;&gt;&quot; 替换为HTML标记
            /// </summary>
            /// <param name="str"></param>
            /// <returns></returns>
            public static string returnHtml(string str)
            {
                if (str.Trim() != "")
                {
                    str = str.Replace(@"&lt;", "<");
                    str = str.Replace(@"&gt;", "<");
                    str = str.Replace(@"&nbsp;", " ");
                    str = str.Replace(@"&quot;", @"""");
                }
                return str;
            }
    
    ///   <summary>
            ///   过滤HTML标记
            ///   </summary>
            ///   <param   name="Htmlstring">包括HTML的源码   </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, @"<!--.*", "", 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 = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "",
                  RegexOptions.IgnoreCase);
                Htmlstring = Regex.Replace(Htmlstring, @"([
    ])[s]+", "",
                  RegexOptions.IgnoreCase);
                Htmlstring.Replace("<", "");
                Htmlstring.Replace(">", "");
                Htmlstring.Replace("
    ", "");
                return Htmlstring;
            }
  • 相关阅读:
    html5 中的 css样式单 的 两种调用方式的区别
    互联网公司的相关人员及业务简介
    require.js 入门学习-备
    IOS修改webView背景透明以及IOS调用前台js的方法
    Javascript AMD模块化规范-备用
    Meta 的两个 相关属性
    <meta http-equiv="pragma" content="no-cache"/>
    css:中文词不断开,整体换行
    linux驱动开发---导出内核符号
    web html 防盗链
  • 原文地址:https://www.cnblogs.com/LJP-JumpAndFly/p/12009181.html
Copyright © 2011-2022 走看看