过滤字符串中的HTML标签

今天看到一个过滤字符串中的HTML标签的函数蛮好的,拿来给大家分享一下嘎嘎

1.      #region 过滤HTML字符串   

2.              /// <summary>   

3.              /// 过滤String中的HTML标签   

4.              /// </summary>   

5.              /// <param name="strHtml">要过滤的html字符串</param>   

6.              /// <returns></returns>   

7.              public string FilterHTML(string strHtml)   

8.              {   

9.                  string[] aryReg ={   

10.                                    @"<script[^>]*?>.*?</script>",   

11.                                    @"<("/"s*)?!?(("w+:)?"w+)("w+("s*=?"s*(([""'])(""[""'tbnr]|[^"7])*?"7|"w+)|.{0})|"s)*?("/"s*)?>",   

12.                                    @"(["r"n])["s]+",   

13.                                    @"&(quot|#34);",   

14.                                    @"&(amp|#38);",   

15.                                    @"&(lt|#60);",   

16.                                    @"&(gt|#62);",   

17.                                    @"&(nbsp|#160);",   

18.                                    @"&(iexcl|#161);",   

19.                                    @"&(cent|#162);",   

20.                                    @"&(pound|#163);",   

21.                                    @"&(copy|#169);",   

22.                                    @"("d+);",   

23.                                    @"-->",   

24.                                    @"<!--.*"n"  

25.     

26.                                };   

27.               string[] aryRep = {   

28.                                     "",   

29.                                     "",   

30.                                     "",   

31.                                     """",   

32.                                     "&",   

33.                                     "<",   

34.                                     ">",   

35.                                     " ",   

36.                                     ""xa1",//chr(161),   

37.                                     ""xa2",//chr(162),   

38.                                     ""xa3",//chr(163),   

39.                                     ""xa9",//chr(169),   

40.                                     "",   

41.                                     ""r"n",   

42.                                     ""  

43.                                 };   

44.               string newReg = aryReg[0];   

45.               string strOutput = strHtml;   

46.               for (int i = 0; i < aryReg.Length; i++)   

47.               {   

48.                   Regex regex = new Regex(aryReg[i], RegexOptions.IgnoreCase);   

49.                   strOutput = regex.Replace(strOutput, aryRep[i]);   

50.               }   

51.               strOutput.Replace("<""");   

52.               strOutput.Replace(">""");   

53.               strOutput.Replace(""r"n""");   

54.     

55.               return strOutput;   

56.           }  

57.           #endregion  

 

 

时隔几日,我发现了另外的一个相同作用的函数,望大家测试一下那个好一点

1.      public static string InnerText(string HTML)   

2.     {   

3.             Regex regEx = new System.Text.RegularExpressions.Regex(@"<"/*[^<>]*>", RegexOptions.IgnoreCase);   

4.             return regEx.Replace(HTML, "");   

5.      }

 

转载于:https://www.cnblogs.com/jimtomjim/archive/2009/05/25/dotnet_string.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值