去掉字符串中的html标签

 1 public static string removeHtml(string html) 
 2 {
 3 
 4     System.Text.RegularExpressions.Regex regex1 = new System.Text.RegularExpressions.Regex(@"<script[sS]+</script *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
 5 
 6     System.Text.RegularExpressions.Regex regex2 = new System.Text.RegularExpressions.Regex(@" href *= *[sS]*script *:", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
 7 
 8     System.Text.RegularExpressions.Regex regex3 = new System.Text.RegularExpressions.Regex(@" no[sS]*=", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
 9 
10     System.Text.RegularExpressions.Regex regex4 = new System.Text.RegularExpressions.Regex(@"<iframe[sS]+</iframe *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
11 
12     System.Text.RegularExpressions.Regex regex5 = new System.Text.RegularExpressions.Regex(@"<frameset[sS]+</frameset *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
13 
14     System.Text.RegularExpressions.Regex regex6 = new System.Text.RegularExpressions.Regex(@"<img[^>]+>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
15 
16     System.Text.RegularExpressions.Regex regex7 = new System.Text.RegularExpressions.Regex(@"</p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
17 
18     System.Text.RegularExpressions.Regex regex8 = new System.Text.RegularExpressions.Regex(@"<p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
19 
20     System.Text.RegularExpressions.Regex regex9 = new System.Text.RegularExpressions.Regex(@"<[^>]*>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
21 
22     html = regex1.Replace(html, ""); //过滤<script></script>标记
23 
24     html = regex2.Replace(html, ""); //过滤href=javascript: (<A>) 属性
25 
26     html = regex3.Replace(html, " _disibledevent="); //过滤其它控件的on...事件
27 
28     html = regex4.Replace(html, ""); //过滤iframe
29 
30     html = regex5.Replace(html, ""); //过滤frameset
31 
32     html = regex6.Replace(html, ""); //过滤frameset
33 
34     html = regex7.Replace(html, ""); //过滤frameset
35 
36     html = regex8.Replace(html, ""); //过滤frameset
37 
38     html = regex9.Replace(html, "");
39 
40     html = html.Replace(" ", "");
41 
42     html = html.Replace("</strong>", "");
43 
44     html = html.Replace("<strong>", "");
45 
46     html = cutBadStr(html);
47 
48     return html.ToUpper();
49 
50 }
51 
52 public static string removeHtmlHasPic(string html) 
53 {
54 
55     System.Text.RegularExpressions.Regex regex1 = new System.Text.RegularExpressions.Regex(@"<script[sS]+</script *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
56 
57     System.Text.RegularExpressions.Regex regex2 = new System.Text.RegularExpressions.Regex(@" href *= *[sS]*script *:", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
58 
59     System.Text.RegularExpressions.Regex regex3 = new System.Text.RegularExpressions.Regex(@" no[sS]*=", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
60 
61     System.Text.RegularExpressions.Regex regex4 = new System.Text.RegularExpressions.Regex(@"<iframe[sS]+</iframe *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
62 
63     System.Text.RegularExpressions.Regex regex5 = new System.Text.RegularExpressions.Regex(@"<frameset[sS]+</frameset *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
64 
65     System.Text.RegularExpressions.Regex regex7 = new System.Text.RegularExpressions.Regex(@"</p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
66 
67     System.Text.RegularExpressions.Regex regex8 = new System.Text.RegularExpressions.Regex(@"<p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
68 
69     System.Text.RegularExpressions.Regex regex9 = new System.Text.RegularExpressions.Regex(@"<[^>]*>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
70 
71     html = regex1.Replace(html, ""); //过滤<script></script>标记
72 
73     html = regex2.Replace(html, ""); //过滤href=javascript: (<A>) 属性
74 
75     html = regex3.Replace(html, " _disibledevent="); //过滤其它控件的on...事件
76 
77     html = regex4.Replace(html, ""); //过滤iframe
78 
79     html = regex5.Replace(html, ""); //过滤frameset
80 
81     html = regex7.Replace(html, ""); //过滤frameset
82 
83     html = regex8.Replace(html, ""); //过滤frameset
84 
85     html = regex9.Replace(html, "");
86 
87     html = html.Replace(" ", "");
88 
89     html = html.Replace("</strong>", "");
90 
91     html = html.Replace("<strong>", "");
92 
93     html = cutBadStr(html);
94 
95     return html;
96 
97 }
原文地址:https://www.cnblogs.com/lanmoxiaozhu/p/3401806.html