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;
            }
  • 相关阅读:
    suse linux编译安装GCC报错
    suse linux 编译安装Apache时报“APR NOT FOUND”的解决方法
    LoadRunner监控Windows和Linux常见问题
    LR报-27727错误解决办法
    主机在virtualbox在NAT方式SSH访问
    清除hao123浏览器劫持小尾巴病毒
    在CentOS上,Servlet出现java.lang.NoClassDefFoundError
    构建第一个SSH的maven项目
    关于Oracle数据库sys用户登入的解惑
    ip route-static 命令的参数
  • 原文地址:https://www.cnblogs.com/LJP-JumpAndFly/p/12009181.html
Copyright © 2011-2022 走看看