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;
            }
  • 相关阅读:
    eclipse中的tomcat中修改部署项目的路径
    failed to load the jni shared library
    Can't call commit when autocommit=true问题的解决方法
    eclipse中快捷键的使用
    Eclipse导入的项目中发现包的形式变成了文件夹的形式,需要将文件夹的形式变成包
    '<>' operator is not allowed for source level below 1.7
    Tomcat中的server.xml配置详解
    shell 常用操作
    Mac 截图保存位置设置
    一周小结(2016-05-23~2016-05-27)
  • 原文地址:https://www.cnblogs.com/LJP-JumpAndFly/p/12009181.html
Copyright © 2011-2022 走看看