• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
小小菜鸟
博客园    首页    新随笔    联系   管理    订阅  订阅
过滤字符串中的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.      }

 

posted on 2009-05-25 09:17  service啊啊  阅读(1160)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3