目录
php过滤html标记属性类用法实例
首页 后端开发 php教程 php过滤html标记属性类用法实例_PHP教程

php过滤html标记属性类用法实例_PHP教程

Jul 13, 2016 am 10:18 AM
html php 具体 实例 属性 方法 标记 用法 过滤

php过滤html标记属性类用法实例

 具体方法如下:

HtmlAttributeFilter.class.php类文件如下:

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       过滤属性 

*  public setAllow      设置允许的属性 

*  public setException    设置特例 

*  public setIgnore     设置忽略的标记 

*  private findElements    搜寻需要处理的元素 

*  private findAttributes   搜寻属性 

*  private removeAttributes  移除属性 

*  private isException    判断是否特例 

*  private createAttributes  创建属性 

*  private protect      特殊字符转义 

*/ 

   

class HtmlAttributeFilter{ // class start 

   

  private $_str = '';      // 源字符串 

  private $_allow = array();   // 允许保留的属性 例如:array('id','class','title') 

  private $_exception = array(); // 特例 例如:array('a'=>array('href','class'),'span'=>array('class')) 

  private $_ignore = array();  // 忽略过滤的标记 例如:array('span','img') 

   

   

  /** 处理HTML,过滤不保留的属性 

  * @param String $str 源字符串 

  * @return String 

  */ 

  public function strip($str){ 

    $this->_str = $str; 

   

    if(is_string($this->_str) && strlen($this->_str)>0){ // 判断字符串 

   

      $this->_str = strtolower($this->_str); // 转成小写 

   

      $res = $this->findElements(); 

      if(is_string($res)){ 

        return $res; 

      } 

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

      $this->removeAttributes($nodes); 

    } 

    return $this->_str; 

  } 

   

  /** 设置允许的属性 

  * @param Array $param 

  */ 

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

    $this->_allow = $param; 

  } 

   

  /** 设置特例 

  * @param Array $param 

  */ 

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

    $this->_exception = $param; 

  } 

   

  /** 设置忽略的标记 

  * @param Array $param 

  */ 

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

    $this->_ignore = $param; 

  } 

   

  /** 搜寻需要处理的元素 */ 

  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; 

    } 

  } 

   

  /** 搜寻属性 

  * @param Array $nodes 需要处理的元素 

  */ 

  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; 

  } 

   

  /** 移除属性 

  * @param Array $nodes 需要处理的元素 

  */ 

  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) ? "" : ""; 

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

    } 

  } 

   

  /** 判断是否特例 

  * @param String $element_name  元素名 

  * @param String $attribute_name 属性名 

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

  } 

   

  /** 创建属性 

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

  } 

   

   

  /** 特殊字符转义 

  * @param String $str 源字符串 

  * @return String 

  */ 

  private function protect($str){ 

    $conversions = array( 

      "^" => "\^",  

      "[" => "\[",  

      "." => "\.",  

      "$" => "\$",  

      "{" => "\{",  

      "*" => "\*",  

      "(" => "\(",  

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

      "/" => "\/",  

      "+" => "\+",  

      ")" => "\)",  

      "|" => "\|",  

      "?" => "\?",  

      " "\

      ">" => "\>"  

    ); 

    return strtr($str, $conversions); 

  } 

   

} // class end 

   

?>

demo示例代码如下:

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 = '

'; 

   

$obj = new HtmlAttributeFilter(); 

   

// 允许id属性 

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

   

$obj->setException(array( 

  'a' => array('href'),  // a 标签允许有 href属性特例 

  'ul' => array('class') // ul 标签允许有 class属性特例 

)); 

   

// img 标签忽略,不过滤任何属性 

$obj->setIgnore(array('img')); 

   

echo 'source str:
'; 

echo htmlspecialchars($str).'

'; 

echo 'filter str:
'; 

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

?>

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/883507.htmlTechArticlephp过滤html标记属性类用法实例 具体方法如下: HtmlAttributeFilter.class.php类文件如下: 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...
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

适用于 Ubuntu 和 Debian 的 PHP 8.4 安装和升级指南 适用于 Ubuntu 和 Debian 的 PHP 8.4 安装和升级指南 Dec 24, 2024 pm 04:42 PM

PHP 8.4 带来了多项新功能、安全性改进和性能改进,同时弃用和删除了大量功能。 本指南介绍了如何在 Ubuntu、Debian 或其衍生版本上安装 PHP 8.4 或升级到 PHP 8.4

如何设置 Visual Studio Code (VS Code) 进行 PHP 开发 如何设置 Visual Studio Code (VS Code) 进行 PHP 开发 Dec 20, 2024 am 11:31 AM

Visual Studio Code,也称为 VS Code,是一个免费的源代码编辑器 - 或集成开发环境 (IDE) - 可用于所有主要操作系统。 VS Code 拥有针对多种编程语言的大量扩展,可以轻松编写

在PHP API中说明JSON Web令牌(JWT)及其用例。 在PHP API中说明JSON Web令牌(JWT)及其用例。 Apr 05, 2025 am 12:04 AM

JWT是一种基于JSON的开放标准,用于在各方之间安全地传输信息,主要用于身份验证和信息交换。1.JWT由Header、Payload和Signature三部分组成。2.JWT的工作原理包括生成JWT、验证JWT和解析Payload三个步骤。3.在PHP中使用JWT进行身份验证时,可以生成和验证JWT,并在高级用法中包含用户角色和权限信息。4.常见错误包括签名验证失败、令牌过期和Payload过大,调试技巧包括使用调试工具和日志记录。5.性能优化和最佳实践包括使用合适的签名算法、合理设置有效期、

php程序在字符串中计数元音 php程序在字符串中计数元音 Feb 07, 2025 pm 12:12 PM

字符串是由字符组成的序列,包括字母、数字和符号。本教程将学习如何使用不同的方法在PHP中计算给定字符串中元音的数量。英语中的元音是a、e、i、o、u,它们可以是大写或小写。 什么是元音? 元音是代表特定语音的字母字符。英语中共有五个元音,包括大写和小写: a, e, i, o, u 示例 1 输入:字符串 = "Tutorialspoint" 输出:6 解释 字符串 "Tutorialspoint" 中的元音是 u、o、i、a、o、i。总共有 6 个元

您如何在PHP中解析和处理HTML/XML? 您如何在PHP中解析和处理HTML/XML? Feb 07, 2025 am 11:57 AM

本教程演示了如何使用PHP有效地处理XML文档。 XML(可扩展的标记语言)是一种用于人类可读性和机器解析的多功能文本标记语言。它通常用于数据存储

解释PHP中的晚期静态绑定(静态::)。 解释PHP中的晚期静态绑定(静态::)。 Apr 03, 2025 am 12:04 AM

静态绑定(static::)在PHP中实现晚期静态绑定(LSB),允许在静态上下文中引用调用类而非定义类。1)解析过程在运行时进行,2)在继承关系中向上查找调用类,3)可能带来性能开销。

什么是PHP魔术方法(__ -construct,__destruct,__call,__get,__ set等)并提供用例? 什么是PHP魔术方法(__ -construct,__destruct,__call,__get,__ set等)并提供用例? Apr 03, 2025 am 12:03 AM

PHP的魔法方法有哪些?PHP的魔法方法包括:1.\_\_construct,用于初始化对象;2.\_\_destruct,用于清理资源;3.\_\_call,处理不存在的方法调用;4.\_\_get,实现动态属性访问;5.\_\_set,实现动态属性设置。这些方法在特定情况下自动调用,提升代码的灵活性和效率。

HTML,CSS和JavaScript的角色:核心职责 HTML,CSS和JavaScript的角色:核心职责 Apr 08, 2025 pm 07:05 PM

HTML定义网页结构,CSS负责样式和布局,JavaScript赋予动态交互。三者在网页开发中各司其职,共同构建丰富多彩的网站。

See all articles