黄聪:PHP使用Simple_HTML_DOM遍历、过滤及保留指定属性
<?
/*
* 参考资料:
* http://www.phpddt.com/manual/simplehtmldom_1_5/manual_api.htm
* http://www.phpddt.com/manual/simplehtmldom_1_5/manual.htm*/ class HtmlUtil{ /*
* $allow:只允许这些属性存在
* $exceptions:一些特殊的元素,可以存在某些属性
*/
public function clear_child_html_attribute( $html_dom, $allow = array(), $exceptions = array() )
{
foreach( $html_dom->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') );
}
}
}
?>
使用方法:
<?
$html = "<p><a href='http://hcsem.com' style='color:#F00;' class='ttt'>黄聪的笔记本</a>还是<b class='test'>很不错</b>的哦!</p>"; $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 ); //输出<p><a href='http://hcsem.com' style='color:#F00;'>黄聪的笔记本</a>还是<b>很不错</b>的哦!</p>
黄聪:PHP使用Simple_HTML_DOM遍历、过滤及保留指定属性的更多相关文章
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(七) Exception Handling Application Block
		原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(七) Exception Handling Application Block 使用企业库异常处理应用程序模块的 ... 
- 【转】黄聪:HtmlAgilityPack教程案例
		[转]黄聪:HtmlAgilityPack教程案例 HtmlAgilityPack中的HtmlNode类与XmlNode类差不多,提供的功能也大同小异.下面来看看该类提供功能. 一.静态属性 publ ... 
- 黄聪:《跟黄聪学WordPress插件开发》
		续<跟黄聪学WordPress主题开发>之后,又一个作品完成!<跟黄聪学Wordpress插件开发>,国内最好的Wordpress插件开发视频教程!! 目录预览: WordPr ... 
- 黄聪:《跟黄聪学WordPress主题开发》
		又一个作品完成!<跟黄聪学Wordpress主题开发>,国内最好的Wordpress主题模版开发视频教程!! 目录预览: WordPress官方源文件层式结构讲解 WordPress数据库 ... 
- 黄聪:VS2010开发如何在c#中使用Ctrl、Alt、Tab等全局组合快捷键
		1.新建一个类 HotkeyHelper using System; using System.Runtime.InteropServices; using System.Windows.Forms ... 
- 黄聪:如何使用CodeSmith批量生成代码(转:http://www.cnblogs.com/huangcong/archive/2010/06/14/1758201.html)
		先看看CodeSmith的工作原理: 简单的说:CodeSmith首先会去数据库获取数据库的结构,如各个表的名称,表的字段,表间的关系等等,之后再根据用户自定义好的模板文件,用数据库结构中的关键字替代 ... 
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(六) Security Application Block
		原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(六) Security Application Block 开发人员经常编写需要安全功能的应用程序.这些应用程序 ... 
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(五) Data Access Application Block
		原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(五) Data Access Application Block 企业库数据库访问模块通过抽象工厂模式,允许用户 ... 
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(四) Logging Application Block
		原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(四) Logging Application Block 企业库日志应用程序模块工作原理图: 从上图我们可以 ... 
随机推荐
- 网站优化之-SEO在网页制作中的应用(信息来自慕课网课程笔记)
			一.SEO基本介绍. 1.搜索引擎工作原理. 2.seo简介:SEarch Engine Optimization,搜索引擎优化.为了提升网页在搜索引擎自然搜索结果中的收录数量及排序位置而做的优化行为 ... 
- ubuntuPC机安装JLink驱动
			摘要: 打开你仿真用的机器人的配置文化,这个应该是local_costmap_params.yaml transform tolerance g改为1 本文介绍了如何在Ubuntu平台配置J- ... 
- 新浪代码部署手册   git管理工具
			目前新浪云上的应用支持通过Git和SVN来部署代码. Git仓库地址 https://git.sinacloud.com/YOUR_APP_NAME SVN仓库地址 https://svn.sinac ... 
- JAVA常用关键字
			Java 中常用关键字: 一一解释(先以印象注明含义,若有错误或未填写的待用到后补充.更新):(蓝色为不确定部分) abstract : 虚类 boolean : 类型定义——布尔型 break : ... 
- Promise 学习笔记
			所谓Promise,简单说就是一个容器,里面保存着某个未来才会结束的事件(通常是一个异步操作)的结果.从语法上说,Promise是一个对象,从它可以获取异步操作的消息.Promise提供统一的API, ... 
- 为什么说Parcelable 比Serializable更高效
			本文转载自:http://blog.csdn.net/androidzhaoxiaogang/article/details/8172539 什么是序列化,实现序列化的目的是什么? 讨论这个问题之前, ... 
- min—width的使用
			在网页中,如果一个元素没有设置最小宽度(min-width),这时当浏览器缩小到一定程度时,元素中的布局可能会发生变化.如果想要保持布局不变,可以给该元素(如div)设置最小宽度属性 .box{ ba ... 
- 万能js实现翻页,动态生成内容自动翻页,兼容各种浏览器(已测试)----神器版!
			转--http://www.2cto.com/kf/201402/277535.html 万能js实现翻页,动态生成内容自动翻页,兼容各种浏览器(已测试)----神器版! 2014-02-11 ... 
- jquery得到iframe src属性值的方法
			这篇文章主要介绍了jquery得到iframe src属性值的方法,很简单,很实用,需要的朋友可以参考下 取得iframe src属性的的值: Html代码 <!DOCTYPE HTML> ... 
- OpenJudge计算概论-计算书费
			/*============================================== 计算书费 总时间限制: 1000ms 内存限制: 65536kB 描述 下面是一个图书的单价表: 计算 ... 
