博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PHP使用Simple_HTML_DOM遍历、过滤及保留指定属性
阅读量:7110 次
发布时间:2019-06-28

本文共 2336 字,大约阅读时间需要 7 分钟。

find('*') as $html_child_dom ) { $this->clear_child_html_attribute( $html_child_dom, $allow, $exceptions ); $this->clear_attribute( $html_child_dom, $allow, $exceptions ); } } public function clear_attribute( $html_dom, $allow = array(), $exceptions = array() ) { //遍历属性 $attrs = $html_dom->getAllAttributes(); if( count( $attrs ) > 0 ) { //遍历属性,进行处理 foreach( $attrs as $attr_key => $attr_value ) { //如果是例外的,则不管 $exceptions_attrs = $exceptions[ $html_dom->tag ]; if( is_array( $exceptions_attrs ) && in_array( $attr_key , $exceptions_attrs ) ){ continue; } //如果不再允许列表中,则删除 if( is_array( $allow ) && in_array( $attr_key , $allow ) ){ continue; } $html_dom->removeAttribute( $attr_key ); } } } public function clear_html_attribute( $html_str, $allow = array(), $exceptions= array() ) { include TEMPLATEPATH . '/class/simple_html_dom.php'; $html = str_get_html( $html_str ); foreach( $html->find( "*" ) as $html_dom ) { //处理所有节点的属性 $this->clear_child_html_attribute( $html_dom, $allow, $exceptions ); } return $html; } function clear_html_post( $post_id ) { if ( ! wp_is_post_revision( $post_id ) ){ remove_action('save_post', array( $this, 'clear_html_post') ); $my_post = get_post( $post_id ); $my_post->post_content = $this->clear_html( $my_post->post_content ); wp_update_post( $my_post ); add_action('save_post', array( $this, 'clear_html_post') ); } }}?>

使用方法:

黄聪的笔记本还是很不错的哦!

";$allow = array( 'style', 'colspan', 'rowspan',);$exceptions = array( 'img' => array( 'src', 'alt' , 'title' , 'width' , 'height' , 'class', 'id' ), 'a' => array( 'href', 'title', 'target'), 'iframe'=>array('src','frameborder'), );global $HtmlUtil;$HtmlUtil = new HtmlUtil;$html = $HtmlUtil->clear_html_attribute( $html, $allow, $exceptions );//输出

黄聪的笔记本还是很不错的哦!

本文转自黄聪博客园博客,原文链接:http://www.cnblogs.com/huangcong/p/5076522.html,如需转载请自行联系原作者
你可能感兴趣的文章
Spring boot 内嵌tomcat,临时目录不存在 错误
查看>>
fedora16中virtualbox无法启动xp虚假机
查看>>
(十五)用JAVA编写MP3解码器——音频输出
查看>>
MyClouds开发指南》第1章 MyClouds微服务治理及快速开发平台简介
查看>>
mysql 时间格式化
查看>>
用JDK制作可能运行的JAR
查看>>
eval 解析 JSON 格式数据
查看>>
hibernate笔记(六)关于懒加载和load()方法之三——误区
查看>>
消息队里-消息队列应用场景
查看>>
推荐:7 月份值得一看的 Java 技术干货!
查看>>
3.7 Struts2综合应用实例--添加学生信息
查看>>
vscode下的eslint自动修复配置
查看>>
31.TCP设置ip 文件秒传
查看>>
Spring 事件Application Event
查看>>
c++类型转换
查看>>
开发人员如何转型做产品经理
查看>>
Ubuntu 下控制台和UI切换
查看>>
6、Kafka问题总结
查看>>
网络计划---网络计划图
查看>>
SVN 基本命令
查看>>