过滤掉 html代码
1 #region 过滤掉 html代码
2 public static string StripHTML(string strHtml)
3 {
4 string [] aryReg ={
5 @"<script[^>]*?>.*?</script>",
6
7 @"<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""'])(\\[""'tbnr]|[^\7])*?\7|\w+)|.{0})|\s)*?(\/\s*)?>",
8 @"([\r\n])[\s]+",
9 @"&(quot|#34);",
10 @"&(amp|#38);",
11 @"&(lt|#60);",
12 @"&(gt|#62);",
13 @"&(nbsp|#160);",
14 @"&(iexcl|#161);",
15 @"&(cent|#162);",
16 @"&(pound|#163);",
17 @"&(copy|#169);",
18 @"&#(\d+);",
19 @"-->",
20 @"<!--.*\n"
21 };
22
23 string [] aryRep = {
24 "",
25 "",
26 "",
27 "\"",
28 "&",
29 "<",
30 ">",
31 " ",
32 "\xa1",//chr(161),
33 "\xa2",//chr(162),
34 "\xa3",//chr(163),
35 "\xa9",//chr(169),
36 "",
37 "\r\n",
38 ""
39 };
40
41 string newReg =aryReg[0];
42 string strOutput=strHtml;
43 for(int i = 0;i<aryReg.Length;i++)
44 {
45 System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(aryReg[i],System.Text.RegularExpressions.RegexOptions.IgnoreCase);
46 strOutput = regex.Replace(strOutput,aryRep[i]);
47 }
48 strOutput.Replace("<","");
49 strOutput.Replace(">","");
50 strOutput.Replace("\r\n","");
51 return strOutput;
52 }
53 #endregion
54
2 public static string StripHTML(string strHtml)
3 {
4 string [] aryReg ={
5 @"<script[^>]*?>.*?</script>",
6
7 @"<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""'])(\\[""'tbnr]|[^\7])*?\7|\w+)|.{0})|\s)*?(\/\s*)?>",
8 @"([\r\n])[\s]+",
9 @"&(quot|#34);",
10 @"&(amp|#38);",
11 @"&(lt|#60);",
12 @"&(gt|#62);",
13 @"&(nbsp|#160);",
14 @"&(iexcl|#161);",
15 @"&(cent|#162);",
16 @"&(pound|#163);",
17 @"&(copy|#169);",
18 @"&#(\d+);",
19 @"-->",
20 @"<!--.*\n"
21 };
22
23 string [] aryRep = {
24 "",
25 "",
26 "",
27 "\"",
28 "&",
29 "<",
30 ">",
31 " ",
32 "\xa1",//chr(161),
33 "\xa2",//chr(162),
34 "\xa3",//chr(163),
35 "\xa9",//chr(169),
36 "",
37 "\r\n",
38 ""
39 };
40
41 string newReg =aryReg[0];
42 string strOutput=strHtml;
43 for(int i = 0;i<aryReg.Length;i++)
44 {
45 System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(aryReg[i],System.Text.RegularExpressions.RegexOptions.IgnoreCase);
46 strOutput = regex.Replace(strOutput,aryRep[i]);
47 }
48 strOutput.Replace("<","");
49 strOutput.Replace(">","");
50 strOutput.Replace("\r\n","");
51 return strOutput;
52 }
53 #endregion
54