Php: html tag attribute class usage _ PHP Tutorial

Source: Internet
Author: User
Php filters html tag attribute class usage instances. The php method for filtering html tag attribute class Usage examples is as follows: the HtmlAttributeFilter. class. php class file is as follows: 1234567891011121314151617181920212223242526 php filters html tag attribute class Usage examples

The specific method is as follows:

The HtmlAttributeFilter. class. php class file is as follows:

1

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

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

/** HTML Attribute Filter

* Date: 2013-09-22

* Author: fdipzone

* Ver: 1.0

*

* Func:

* Public strip filter attributes

* Public setAllow: Set the allowed attributes.

* Special case for setting public setException

* Public setIgnore: Set the ignored flag.

* Private findElements: Search for elements to be processed

* Private findAttributes search attributes

* Private removeAttributes: remove attributes

* Private isException: determines whether a special case exists.

* Private createAttributes: create attributes

* Private protect special character escape

*/

Class HtmlAttributeFilter {// class start

Private $ _ str = ''; // source string

Private $ _ allow = array (); // attributes that can be retained, for example, array ('id', 'class', 'title ')

Private $ _ exception = array (); // special example: array ('a' => array ('href ', 'class '), 'span '=> array ('class '))

Private $ _ ignore = array (); // ignore filtered tags, for example, array ('span ', 'IMG ')

/** Process HTML and filter attributes that are not retained

* @ Param String $ str Source String

* @ Return String

*/

Public function strip ($ str ){

$ This-> _ str = $ str;

If (is_string ($ this-> _ str) & strlen ($ this-> _ str)> 0) {// judge a string

$ This-> _ str = strtolower ($ this-> _ str); // Convert it to lowercase.

$ Res = $ this-> findElements ();

If (is_string ($ res )){

Return $ res;

}

$ Nodes = $ this-> findAttributes ($ res );

$ This-> removeAttributes ($ nodes );

}

Return $ this-> _ str;

}

/** Set allowed attributes

* @ Param Array $ param

*/

Public function setAllow ($ param = array ()){

$ This-> _ allow = $ param;

}

/** Set special cases

* @ Param Array $ param

*/

Public function setException ($ param = array ()){

$ This-> _ exception = $ param;

}

/** Set the ignored flag

* @ Param Array $ param

*/

Public function setIgnore ($ param = array ()){

$ This-> _ ignore = $ param;

}

/** Search for elements to be processed */

Private function findElements (){

$ Nodes = array ();

Preg_match_all ("/<([^! \/\> \ N] +) ([^>] *)>/I ", $ this-> _ str, $ elements );

Foreach ($ elements [1] as $ el_key => $ element ){

If ($ elements [2] [$ el_key]) {

$ Literal = $ elements [0] [$ el_key];

$ Element_name = $ elements [1] [$ el_key];

$ Attributes = $ elements [2] [$ el_key];

If (is_array ($ this-> _ ignore )&&! In_array ($ element_name, $ this-> _ ignore )){

$ Nodes [] = array ('literal' => $ literal, 'name' => $ element_name, 'bubuckets' => $ attributes );

}

}

}

If (! $ Nodes [0]) {

Return $ this-> _ str;

} Else {

Return $ nodes;

}

}

/** Search for attributes

* @ Param Array $ elements to be processed by nodes

*/

Private function findAttributes ($ nodes ){

Foreach ($ nodes as & $ node ){

Preg_match_all ("/([^ =] +) \ s * = \ s * [\" | '] {0, 1} ([^ \ "'] *) [\ "| '] {0, 1}/I", $ node ['bubuckets'], $ attributes );

If ($ attributes [1]) {

Foreach ($ attributes [1] as $ att_key => $ att ){

$ Literal = $ attributes [0] [$ att_key];

$ Attribute_name = $ attributes [1] [$ att_key];

$ Value = $ attributes [2] [$ att_key];

$ Atts [] = array ('literal' => $ literal, 'name' => $ attribute_name, 'value' => $ value );

}

} Else {

$ Node ['bubuckets'] = null;

}

$ Node ['bubuckets'] = $ atts;

Unset ($ atts );

}

Return $ nodes;

}

/** Remove attributes

* @ Param Array $ elements to be processed by nodes

*/

Private function removeAttributes ($ nodes ){

Foreach ($ nodes as $ node ){

$ Node_name = $ node ['name'];

$ New_attributes = '';

If (is_array ($ node ['bubuckets']) {

Foreach ($ node ['buckets'] as $ attribute ){

If (is_array ($ this-> _ allow) & in_array ($ attribute ['name'], $ this-> _ allow )) | $ this-> isException ($ node_name, $ attribute ['name'], $ this-> _ exception )){

$ New_attributes = $ this-> createAttributes ($ new_attributes, $ attribute ['name'], $ attribute ['value']);

}

}

}

$ Replacement = ($ new_attributes )? "<$ Node_name $ new_attributes>": "<$ node_name> ";

$ This-> _ str = preg_replace ('/'. $ this-> protect ($ node ['literal']). '/', $ replacement, $ this-> _ str );

}

}

/** Determine whether a special case exists

* @ Param String $ element_name element name

* @ Param String $ attribute_name attribute name

* @ Param Array $ special exceptions

* @ Return boolean

*/

Private function isException ($ element_name, $ attribute_name, $ exceptions ){

If (array_key_exists ($ element_name, $ this-> _ exception )){

If (in_array ($ attribute_name, $ this-> _ exception [$ element_name]) {

Return true;

}

}

Return false;

}

/** Create attributes

* @ Param String $ new_attributes

* @ Param String $ name

* @ Param String $ value

* @ Return String

*/

Private function createAttributes ($ new_attributes, $ name, $ value ){

If ($ new_attributes ){

$ New_attributes. = "";

}

$ New_attributes. = "$ name = \" $ value \"";

Return $ new_attributes;

}

/** Escape special characters

* @ Param String $ str Source String

* @ Return String

*/

Private function protect ($ str ){

$ Conversions = array (

"^" => "\ ^ ",

"[" => "\[",

"." => "\.",

"$" => "\ $ ",

"{" => "\{",

"*" => "\*",

"(" => "\(",

"\\" => "\\\\",

"/" => "\/",

"+" => "\ + ",

")" => "\)",

"|" => "\ | ",

"? "=> "\? ",

"<" => "\ <",

">" => "\>"

);

Return strtr ($ str, $ conversions );

}

} // Class end

?>

The demo code is as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

Require ('htmlattributefilter. class. php ');

$ Str ='

  • Yuna

    Love

    Want to knowYES

';

$ Obj = new HtmlAttributeFilter ();

// Allow the id attribute

$ Obj-> setAllow (array ('id '));

$ Obj-> setException (array (

'A' => array ('href '), // a tag allows special cases with the href attribute

'Ul '=> array ('class') // The ul tag allows special class attribute exceptions.

));

// The img label is ignored and no attributes are filtered.

$ Obj-> setIgnore (array ('IMG '));

Echo 'source str:
';

Echo htmlspecialchars ($ str ).'

';

Echo 'filter str:
';

Echo htmlspecialchars ($ obj-> strip ($ str ));

?>

The specific method of filters is as follows: HtmlAttributeFilter. class. php files are as follows: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 19 20 21 22 24 25 26...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.