PHP Filter HTML markup Attribute Class usage Example _php tutorial

Source: Internet
Author: User

PHP Filter HTML markup attribute class usage instance


Here's how:

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

21st

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 Properties

* Public Setallow setting allowed properties

* Public SetException Set exceptions

* Public Setignore Set ignored tags

* Private findelements search for elements to be processed

* Private Findattributes Search Properties

* Private Removeattributes Removal properties

* Private Isexception judge whether special

* Private Createattributes Create attribute

* Private protect special character escapes

*/

Class htmlattributefilter{//Class start

Private $_str = '; SOURCE string

Private $_allow = Array (); Allowed attributes such as: Array (' ID ', ' class ', ' title ')

Private $_exception = Array (); Exceptions For example: Array (' A ' =>array (' href ', ' class '), ' span ' =>array (' class '))

Private $_ignore = Array (); Ignore filtered tags such as: Array (' span ', ' img ')

/** processing HTML, filtering non-reserved properties

* @param string $STR source strings

* @return String

*/

Public function strip ($STR) {

$this->_str = $str;

if (is_string ($this->_str) && strlen ($this->_str) >0) {//Judgment string

$this->_str = strtolower ($this->_str); Turn into lowercase

$res = $this->findelements ();

if (is_string ($res)) {

return $res;

}

$nodes = $this->findattributes ($res);

$this->removeattributes ($nodes);

}

return $this->_str;

}

/** Setting the Allowed properties

* @param Array $param

*/

Public Function Setallow ($param =array ()) {

$this->_allow = $param;

}

/** Setting exceptions

* @param Array $param

*/

Public Function SetException ($param =array ()) {

$this->_exception = $param;

}

/** Setting ignored tags

* @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, ' attributes ' = $attributes);

}

}

}

if (! $nodes [0]) {

return $this->_str;

}else{

return $nodes;

}

}

/** Search Properties

* @param Array $nodes The element to be processed

*/

Private Function Findattributes ($nodes) {

foreach ($nodes as & $node) {

Preg_match_all ("/([^ =]+) \s*=\s*[\" |] {0,1} ([^\"']*) [\"|'] {0,1}/i ", $node [' Attributes '], $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 [' attributes '] = NULL;

}

$node [' attributes '] = $atts;

Unset ($atts);

}

return $nodes;

}

/** Removing properties

* @param Array $nodes The element to be processed

*/

Private Function Removeattributes ($nodes) {

foreach ($nodes as $node) {

$node _name = $node [' name '];

$new _attributes = ";

if (Is_array ($node [' attributes '])) {

foreach ($node [' attributes '] 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);

}

}

/** judge whether the exception

* @param String $element _name element name

* @param String $attribute _name Property name

* @param Array $exceptions allowed 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;

}

/** Creating properties

* @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;

}

/** Special Character escapes

* @param string $STR source strings

* @return String

*/

Private function Protect ($STR) {

$conversions = Array (

"^" = "\^",

"[" = "\[",

"." = "\.",

"$" = "\$",

"{" = "\{",

"*" = "\*",

"(" = "\ (",

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

"/" = "\",

"+" = "\+",

")" = "\)",

"|" = "\|",

"?" = "\",

"<" = "\<",

">" + "\>"

);

Return Strtr ($str, $conversions);

}

}//Class end

?>

The demo sample code is as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

22

23

Require (' HtmlAttributeFilter.class.php ');

$str = '

  • Yuna

    Love

    want to know YES
';

$obj = new Htmlattributefilter ();

Allow ID attribute

$obj->setallow (Array (' ID '));

$obj->setexception (Array (

' A ' + = Array (' href '),//a tag with href attribute exception allowed

The ' ul ' = = Array (' class ')//UL tag allows a special case of class attribute

));

IMG tag ignored, no attributes are filtered

$obj->setignore (Array (' IMG '));

echo ' Source str:
';

echo Htmlspecialchars ($str). '

';

Echo ' Filter str:
';

Echo Htmlspecialchars ($obj->strip ($STR));

?>

http://www.bkjia.com/PHPjc/883507.html www.bkjia.com true http://www.bkjia.com/PHPjc/883507.html techarticle PHP Filter HTML markup Attribute Class usage examples are 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 2 2 ...

  • 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.