无为清净楼资源网 Design By www.qnjia.com
本文实例讲述了php 过滤html标记属性类及其用法。是PHP项目开发中比较常见的实用技巧。分享给大家供大家参考。具体方法如下:
HtmlAttributeFilter.class.php类文件如下:
<"/<([^ !\/\>\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) "<$node_name $new_attributes>" : "<$node_name>"; $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 "htmlcode"><"bd clearfix" id="index_hilite_ul"><ul class="list"><li><img src="/UploadFiles/2021-04-02/logo_white.png">本机下载。希望本文所述对大家的PHP程序设计有所帮助。
无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com
暂无评论...
更新日志
2024年09月23日
2024年09月23日
- 金池刘罡《昨日情歌HQ》头版限量编号[低速原抓WAV+CUE]
- 段奥娟.2024-23·岁碎念【白米范】【FLAC分轨】
- 顺子.2001-昨日·唯一·更多·顺子自选辑【魔岩】【WAV+CUE】
- 无印良品.1999-私藏三首·原创相对论(EP)【滚石】【WAV+CUE】
- Merryland《Merryland》[FLAC/分轨][170.22MB]
- 庄锭欣 ELKIE《没有你的未来》[320K/MP3][15.78MB]
- 庄锭欣 ELKIE《没有你的未来》[FLAC/分轨][36MB]
- TchaikovskySwanLake,Op.20柴可夫斯基天鹅湖(2013)SACD[Hi-Res]
- 24bit达人艺典《琴歌终极版》整轨WAV+CUE
- 黑鸭子.2005-《发烧DSD》柏菲[WAV分轨]
- 群星.1990-电影看板·人间道【宝丽金】【WAV+CUE】
- 周殷廷.2024-TO.WHOM.IT.MAY.CONCERN【环球】【FLAC分轨】
- 草蜢.1997-三人世界精选17首【宝丽金】【WAV+CUE】
- 【古琴音乐】管平湖《广陵散》2004[FLAC+CUE]
- 【中国民乐】《少数民族器乐·苗岭的早晨》2004[WAV+CUE]