今天看到一个过滤字符串中的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. }