C# 删除String中的HTML标记的类(转载)

 1 using  System;
 2 using  System.Data;
 3 using  System.Configuration;
 4 using  System.Web;
 5 using  System.Text.RegularExpressions;
 6
 7 namespace  ConsoleApplication1
 8 {
 9    class Class1
10    {
11        /**//// <summary>
12        /// 去除HTML标记
13        /// </summary>
14        /// <param name="strHtml">包括HTML的源码 </param>
15        /// <returns>已经去除后的文字</returns>

16        public static string StripHTML(string strHtml)
17        {
18            string[] aryReg ={
19          @"<script[^>]*?>.*?</script>",
20
21          @"<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""'])(file://[""'tbnr]|[^/7])*?/7|/w+)|.{0})|/s)*?(///s*)?>",
22          @"([\r\n])[\s]+",
23          @"&(quot|#34);",
24          @"&(amp|#38);",
25
26          @"&(lt|#60);",
27          @"&(gt|#62);"
28          @"&(nbsp|#160);"
29          @"&(iexcl|#161);",
30          @"&(cent|#162);",
31          @"&(pound|#163);",
32          @"&(copy|#169);",
33          @"(\d+);",
34          @"-->",
35          @"<!--.*\n"
36         
37         }
;
38
39            string[] aryRep = {
40           "",
41           "",
42           "",
43           "\"",
44           "&",
45           "<",
46           ">",
47           " ",
48           "\xa1",//chr(161),
49           "\xa2",//chr(162),
50           "\xa3",//chr(163),
51           "\xa9",//chr(169),
52           "",
53           "\r\n",
54           ""
55          }
;
56
57            string newReg = aryReg[0];
58            string strOutput = strHtml;
59            for (int i = 0; i < aryReg.Length; i++)
60            {
61                Regex regex = new Regex(aryReg[i], RegexOptions.IgnoreCase);
62                strOutput = regex.Replace(strOutput, aryRep[i]);
63            }

64
65            strOutput.Replace("<""");
66            strOutput.Replace(">""");
67            strOutput.Replace("\r\n""");
68
69
70            return strOutput;
71        }

72    }

73}

74
75
76

转载于:https://www.cnblogs.com/mishy/archive/2008/01/03/1024953.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值