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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76
