黄聪: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 企业库日志应用程序模块工作原理图: 从上图我们可以 ...
随机推荐
- 隐马尔科夫模型(HMM)的概念
定义隐马尔科夫模型可以用一个三元组(π,A,B)来定义:π 表示初始状态概率的向量A =(aij)(隐藏状态的)转移矩阵 P(Xit|Xj(t-1)) t-1时刻是j而t时刻是i的概率B =(bij) ...
- Android——ListView
1.ArryAdapter: arry_adapter的layout文件: <?xml version="1.0" encoding="utf-8"?&g ...
- SysTick 定时器的使用
SysTick是STM32中的一个24位的定时器. Cortex‐M3处理器内部包含了一个简单的定时器.因为所有的CM3芯片都带有这个定时器,软件在不同 CM3器件间的移植工作得以化简.该定时器的时钟 ...
- when compile /home/wangxiao/NVIDIA-CUDA-7.5 SAMPLES, it warning: gcc version larger than 4.9 not supported, so: old verson of gcc and g++ are needed
1. when compile /home/wangxiao/NVIDIA-CUDA-7.5 SAMPLES, it warning: gcc version larger than 4.9 not ...
- Linux-内核缓存区和write行为
<Unix环境高级编程> 应用缓冲技术能很明显的提高系统效率.内核与外围设备的数据交换,内核与用户空间的数据交换都是比较费时的,使用缓冲区就是为了优化这些费时的操作.其实核心到用户空间的操 ...
- Android调用系统分享功能以及createChooser的使用
工程结构 //效果图 点击测试分享 ...
- PHP包名解释
下载地址: http://windows.php.net/download#php-5.5 下载到的php*.zip各项文件说明: php-5.6.26-nts-Win32-VC11-x ...
- noip2011普及组——数字反转
数字反转 时间限制:1s 内存限制:128MB[问题描述]给定一个整数,请将该数各个位上数字反转得到一个新数.新数也应满足整数的常见形式,即除非给定的原数为零,否则反转后得到的新数的最高位数字不应为零 ...
- wikioi 1203 判断浮点数是否相等
/*======================================================================== 1203 判断浮点数是否相等 题目描述 Descr ...
- js停止冒泡和阻止浏览器默认行为
停止冒泡通用方法: function stopBubble(e) { //如果提供了事件对象,是非IE浏览器 if ( e && e.stopPropagation ) //使用W3C ...