- 浏览: 13640426 次
- 性别:
- 来自: 洛杉矶
文章分类
- 全部博客 (1994)
- Php / Pear / Mysql / Node.js (378)
- Javascript /Jquery / Bootstrap / Web (435)
- Phone / IOS / Objective-C / Swift (137)
- Ubuntu / Mac / Github / Aptana / Nginx / Shell / Linux (335)
- Perl / Koha / Ruby / Markdown (8)
- Java / Jsp (12)
- Python 2 / Wxpython (25)
- Codeigniter / CakePHP (32)
- Div / Css / XML / HTML5 (179)
- WP / Joomla! / Magento / Shopify / Drupal / Moodle / Zimbra (275)
- Apache / VPN / Software (31)
- AS3.0/2.0 / Flex / Flash (45)
- Smarty (6)
- SEO (24)
- Google / Facebook / Pinterest / SNS (80)
- Tools (22)
最新评论
-
1455975567:
xuezhongyu01 写道wocan23 写道我想问下那个 ...
Mysql: LBS实现查找附近的人 (两经纬度之间的距离) -
xuezhongyu01:
wocan23 写道我想问下那个111.1是怎么得来的我也看不 ...
Mysql: LBS实现查找附近的人 (两经纬度之间的距离) -
18335864773:
试试 pageoffice 在线打开 PDF 文件吧. pag ...
jquery在线预览PDF文件,打开PDF文件 -
青春依旧:
opacity: 0.5; 个人喜欢这种方式!关于其他css特 ...
css透明度的设置 (兼容所有浏览器) -
July01:
推荐用StratoIO打印控件,浏览器和系统的兼容性都很好,而 ...
搞定网页打印自动分页问题
类:
<?php function reg_escape( $str ) { $conversions = array( "^" => "\^", "[" => "\[", "." => "\.", "$" => "\$", "{" => "\{", "*" => "\*", "(" => "\(", "\\" => "\\\\", "/" => "\/", "+" => "\+", ")" => "\)", "|" => "\|", "?" => "\?", "<" => "\<", ">" => "\>" ); return strtr( $str, $conversions ); } /** * Strip attribute Class * Remove attributes from XML elements * @author David (semlabs.co.uk) * @version 0.2.1 */ class cleanHtml{ public $str = ''; public $allow = array(); public $exceptions = array(); public $ignore = array(); public function strip( $str ) { $this->str = $str; if( is_string( $str ) && strlen( $str ) > 0 ) { $res = $this->findElements(); if( is_string( $res ) ) return $res; $nodes = $this->findAttributes( $res ); $this->removeAttributes( $nodes ); } return $this->str; } private function findElements() { # Create an array of elements with attributes $nodes = array(); preg_match_all( "/<([^ !\/\>\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 ); } } # Return the XML if there were no attributes to remove if( !$nodes[0] ) return $this->str; else return $nodes; } private function findAttributes( $nodes ) { # Extract attributes 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; } private function removeAttributes( $nodes ) { # Remove unwanted attributes foreach( $nodes as $node ) { # Check if node has any attributes to be kept $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->exceptions ) ) $new_attributes = $this->createAttributes( $new_attributes, $attribute['name'], $attribute['value'] ); } } $replacement = ( $new_attributes ) ? "<$node_name $new_attributes>" : "<$node_name>"; $this->str = preg_replace( '/'. reg_escape( $node['literal'] ) .'/', $replacement, $this->str ); } } private function isException( $element_name, $attribute_name, $exceptions ) { if( array_key_exists($element_name, $this->exceptions) ) { if( in_array( $attribute_name, $this->exceptions[$element_name] ) ) return true; } return false; } private function createAttributes( $new_attributes, $name, $value ) { if( $new_attributes ) $new_attributes .= " "; $new_attributes .= "$name=\"$value\""; return $new_attributes; } }
实例:
$str = 'Here is some sample html that is <span id="good" class="aClass" style="abc" font="3"> <font color="red"> getting broken </font> </span> <iframe width="540" height="304" src="http://www.youtube.com/embed/YacZqlFz2bI?fs=1&feature=oembed" frameborder="0" allowfullscreen></iframe> <img id="featuredon" src="http://www.goodfinancialcents.com/wp-content/themes/thesis_182/custom/images/featuredon.jpg" height="23" width="265" />'; $sa = new cleanHtml; $sa->allow = array( 'id' ); $sa->exceptions = array( 'img' => array( 'src', 'alt' ), 'a' => array( 'href', 'title' ), 'iframe'=>array('src','frameborder'), ); echo $str = $sa->strip( $str );
来源:semlabs.co.uk
发表评论
-
PHP: 在类(class)中加载动态函数, 变量函数或半变量函数 variable function/method
2016-09-03 07:54 7102最终实例在下方 以前 ... -
MySQL入门 (七) : 储存引擎与资料型态
2016-09-03 07:49 45031 表格与储存引擎 表格(table)是资料库中用来储存 ... -
MySQL入门 (六) : 字元集与资料库
2016-09-03 07:47 45101 Character Set与Collation 任何 ... -
MySQL入门 (五) : CRUD 与资料维护
2016-09-03 07:46 54241 取得表格资讯 1.1 DESCRIBE指令 「 ... -
MySQL入门 (四) : JOIN 与UNION 查询
2016-09-03 07:42 44431 使用多个表格 在「world」资料库的「countr ... -
PHP: 关键字global 和 超全局变量$GLOBALS的用法、解释、区别
2016-08-31 12:07 4983$GLOBALS 是一个关联数组,每一个变量为一个 ... -
MySQL入门 (三) : 运算式与函式
2016-08-31 12:01 4332运算式(expressions)已经 ... -
MySQL入门 (二) : SELECT 基础查询
2016-08-31 11:56 46371 查询资料前的基本概念 1.1 表格、纪录 ... -
MySQL入门 (一) : 资料库概论与MySQL的安装
2016-08-31 11:51 43961. 储存与管理资料 储存与管理资料一直是资讯应用上最基本 ... -
MySQL入门 (九) : 子查询 Subquery
2016-08-30 02:26 44001 一个叙述中的查询叙述 子查询(subquery)是一 ... -
PHP: 用readonly取代disabled来获取input值 submit a disabled input in a form could not ge
2016-08-30 02:21 2654The form is like below; <f ... -
PHP7革新与性能优化
2016-08-30 02:20 2098有幸参与2015年的PHP技 ... -
Mysql: 图解 inner join、left join、right join、full outer join、union、union all的区别
2016-08-18 06:03 3073对于SQL的Join,在学习起来可能是比较乱的。我们知道, ... -
Comet 反Ajax: 基于jQuery与PHP实现Ajax长轮询(LongPoll)
2016-08-18 06:00 1331传统的AJAX轮询方式,客服端以用户定义的时间间隔去服务器上 ... -
PHP:ServerPush (Comet推送) 技术的探讨
2016-08-18 05:58 993PHP中Push(推送)技术的探讨 [http://vi ... -
PHP: 手把手编写自己的 MVC 框架实例教程
2016-08-16 05:33 17631 什么是MVC MVC模式(Model-View-Con ... -
PHP5: mysqli 插入, 查询, 更新和删除 Insert Update Delete Using mysqli (CRUD)
2016-07-29 12:55 1703原文: PHP5: mysqli 插入, 查询, 更新 ... -
MongoDB 教程索引 (附有视频)
2016-07-27 10:54 696MongoDB 教程索引 MongoDB 教程一: ... -
Node.js 模块之Nimble流程控制
2016-07-18 12:59 1041NodeJS异步的特性有时候 ... -
Node.js web应用模块之Supervisor
2016-07-18 12:56 2286在开发或调试Node.js应 ...
相关推荐
Laravel开发-cleanhtml 快速轻松地清除HTML文本,确保只留下最少量的内容
软件简介 ~~~~~~~~ HTML文件丰富的标记带给我们的并不总是方便,尤其是当你将HTML文件从互联网上保存下来,离线浏览时问题就很多了。由于页面上的图片不存在而显示带着红叉的大片空白;...可以记录个性化参数设置。
CleanMem微型监视器是一个补充与CleanMem的作品,它不会取代CleanMem.CleanMem仍然是一个程序设置和忘记。现在的CleanMem迷你显示器为用户提供了更多选择,一个简单的方法来保存记忆的眼睛和运行的一个按钮CleanMem...
基于CBF的CLEAN-SC算法详述与MATLAB程序
Functional Programming in CLEAN
The Clean Architecture in PHP 英文无水印原版pdf pdf所有页面使用FoxitReader、PDF-XChangeViewer、SumatraPDF和Firefox测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细...
微软书籍Write Clean Code 微软书籍Write Clean Code 微软书籍Write Clean Code 微软书籍Write Clean Code
Clean.Architecture.pdf Clean.Architecture.pdf Clean.Architecture.pdf
Clean Zone响应设计后台管理HTML5模板 Clean Zone是一个响应管理模板基于Bootstrap 3它有一个整洁的设计和美丽的特性可以使用了。
波束形成CLEAN算法,带PDF详细介绍
这个文件是 clean code 的一个大概介绍 帮助你写更模块化 和更专业 的PHP代码。
Eclipse Clean Up 模板,在Eclipse设置内导入此文件
内存少并不一定就慢,而是在你窗口越开越多,某些程序比如FireFox把内存都占用了,Windows开始使用pagefile来弥补内存的时候,你的电脑才会变得越来越...http://download.cnet.com/CleanMem/3000-18512_4-11673091.html
import cleanHtml from "clean-html-js" ; const url = "https://www.a11ywatch.com" ; async function grabReaderData ( ) { const source = await fetch ( url ) ; const html = await source . text ( ) ; ...
Windows Installer CleanUp Utility可以做到: - 软件将显示出一个列出了当前经Windows Installer安装上去的程序列表,您可以选中您软件名称然后删掉此软件的Windows Installer安装信息。 - 移除选定软件在文件和...
Windows Install Clean Up御载工具,可以彻底解决你安装中的困扰,比如你电脑装了好多.net framewok版本,想删掉其中一个版本。
Clean Zone - Responsive Admin Template
clean.bat clean 清理文件内容使用
CLEAN算法实现超宽带信道估计 的matlab仿真全代码,保过若干个M文件以及mat数据文件,使用时需先load格式为mat的数据文件。
已知输入,输出信号利用clean算法获取信道冲激函数,并验证冲激函数的准确性,输入与冲激函数卷积能得到输出