zoukankan      html  css  js  c++  java
  • 过滤字符串html标签方法

    过滤字符串html标签方法,如果输入的过滤标签为“*”,那么给字符串加上p标签

     public static string noTagHtml(string str, string tagname)
            {
                string zz = @"(<" + tagname + ".*?>)|(</" + tagname + ">)";
                if (tagname == "script") zz = "(<" + tagname + ".*?>)*(</" + tagname + ">)";
                //过滤除P标签的所有标签,不清除P标签里css样式
                else if (tagname == "p")
                {
                    //zz = @"</?s*(?!p|br)+[^>]*>";
                    zz = @"</?s*(?!br)+[^>]*>";
                    Regex r = new Regex(zz, RegexOptions.IgnoreCase | RegexOptions.Singleline);
                    str = Regex.Replace(str, zz, "", RegexOptions.IgnoreCase);
                    str = r.Replace(str, "");
                }
    
                else if (tagname == "*")
                {
                    zz = @"(<([^>]*)>)";
                    Regex r = new Regex(zz, RegexOptions.IgnoreCase | RegexOptions.Singleline);
                    str = Regex.Replace(str, zz, "", RegexOptions.IgnoreCase);
                    str = r.Replace(str, "");
                    Regex regex1 = new Regex(@"
    ", RegexOptions.IgnoreCase);
                    str = regex1.Replace(str, "");
                    Regex regex2 = new Regex(@"
    [s| ]*
    ", RegexOptions.IgnoreCase);
                    str = regex2.Replace(str, "");
                    string[] hangCounts = str.Split('
    ');
    
                    string strTempList = "";
                    for (int k = 0; k < hangCounts.Length; k++)
                    {
                        if (!strTempList.Equals(""))
                        {
                            strTempList = strTempList + "";
                        }
                        if (!hangCounts[k].Equals(""))
                        {
                            strTempList = strTempList + "<p>" + hangCounts[k] + "</p>
    ";
                        }
                    }
                    strTempList = strTempList + "";
                    str = strTempList;
                }
                else
                {
                   
                    Regex r = new Regex(zz, RegexOptions.IgnoreCase | RegexOptions.Singleline);
                    str = Regex.Replace(str, zz, "", RegexOptions.IgnoreCase);
                    str = r.Replace(str, "");
    
                }
                return (str);
    
            }
  • 相关阅读:
    学习asp.net完整步骤
    UltraEdit中Matlab语法高亮显示的操作方法
    C#基础完成和深入
    75道程序员面试逻辑思维题
    模拟退火算法解决函数优化问题
    模拟退火算法解决TSP问题
    C#入门
    简单感知器模型解决简单真值表问题
    jQuery 1.3.2 :visible选择器问题
    NUnit2.0详细使用方法
  • 原文地址:https://www.cnblogs.com/xuxiaoshuan/p/3591201.html
Copyright © 2011-2022 走看看