黄聪: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 企业库日志应用程序模块工作原理图: 从上图我们可以 ...
随机推荐
- POJ 3461 裸的KMP
直接贴代码吧 #include<cstdio> #include<cstring> ],T[]; ]; int n,m; void getfail() { f[] = ; f[ ...
- JavaScript学习记录总结(六)——js函数闭包特性
<script type="text/javascript"> function arrdemo(){ var arr=["hello&q ...
- LB负载均衡集群及DR模式配置
一.系统环境准备: 1.dir服务器 主机名称:dir 系统环境:CentOS release 6.5 (Final) 外网ip:192.168.1.203(网络模式桥接) vip:192.168.1 ...
- Android实现拖动进度条改变图片透明度
layout文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:an ...
- Android—常用组件练习
新建一个文件“practice1.xml” 编写代码如下: <?xml version="1.0" encoding="utf-8"?> <L ...
- strlen() 函数
strlen() 函数通常用来计算字符串的长度,但是今天突然发现个奇怪的现象. 如下所示: #include <stdio.h> #include <stdlib.h> #in ...
- python模块结构和布局
用模块来合理的组织你的python代码是简单又自然的方法.下面介绍一种非常合理的布局: #(1)起始行(Unix) #(2)模块文档 #(3)模块导入 #(4)变量定义 #(5)类定义 #(6)函数定 ...
- Q7: Unique Binary Search Trees
问题描述: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For ...
- Nancy 简单学习
1. 环境 vs2010 , nancy 2. 需要测试的功能 1. 输出 页面输出简单文本 2. 输出json 数据 3. 操作 1. 创建asp.net 空项目 2. 添加引用Nancy库 使用n ...
- 在eclipse中的tomcat内存设置
设置步骤如下: 1.点击eclipse上的debug图标旁边的下拉箭头 2.然后选择Run Configurations, 3.系统弹出设置tomcat配置页面,在Argument中末尾添加参数中的V ...