当前位置:编程学习 > ASP.NET> 正文

asp.net去除字符串中html标签

时间:2013-9-17类别:编程学习

asp.net去除字符串中html标签

asp.net去除字符串中html标签

具体代码如下:

 

  •  
  • C# 代码   复制
  • 
    using System.Text.RegularExpressions;
    
    public static string ClearHTMLTag(string strHtml)
    {
       string[] arrayReg ={
              @"<script[^>]*?>.*?</script>",
    
              @"<(\\/\\s*)?!?((\\w+:)?\\w+)(\\w+(\\s*=?\\s*(([""'])(file://[""'tbnr]|[^/7])*?/7|/w+)|.{0})|/s)*?(///s*)?>",
              @"([\\r\\n])[\\s]+",
              @"&(quot|#34);",
              @"&(amp|#38);",
              @"&(lt|#60);",
              @"&(gt|#62);", 
              @"&(nbsp|#160);", 
              @"&(iexcl|#161);",
              @"&(cent|#162);",
              @"&(pound|#163);",
              @"&(copy|#169);",
              @"&#(\\d+);",
              @"-->",
              @"<!--.*\\n"
             
             };
    
       string[] arrayRep = {
               "",
               "",
               "",
               "\\"",
               "&",
               "<",
               ">",
               " ",
               "\\xa1",//chr(161),
               "\\xa2",//chr(162),
               "\\xa3",//chr(163),
               "\\xa9",//chr(169),
               "",
               "\\r\\n",
               ""
              };
    
       string strReturn = strHtml;
       for (int i = 0; i < arrayReg.Length; i++)
       {
          Regex regex = new Regex(arrayReg[i], RegexOptions.IgnoreCase);
          strReturn = regex.Replace(strReturn , arrayRep[i]);
       }
    
       strReturn = strReturn.Replace("<", "");
       strReturn = strReturn.Replace(">", "");
       strReturn = strReturn.Replace(\\r\\n, "");
    
    
       return strReturn;
    }
    			

     

  •  

    上一篇下一篇

    猜您喜欢

    热门推荐