龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

php过滤html标记属性类用法实例(2)

时间:2014-09-25 11:45来源:网络整理 作者:网络 点击:
分享到:
demo示例代码如下: php require('HtmlAttributeFilter.class.php'); $str = 'div class="bd clearfix" id="index_hilite_ul"ul class="list"liimg src="http://www.1sohu.com/sp/attachments/allimg/140925/

demo示例代码如下:

<?php 
require('HtmlAttributeFilter.class.php'); 
 
$str = '<div class="bd clearfix" id="index_hilite_ul"><ul class="list"><li><img src="http://www.1sohu.com/sp/attachments/allimg/140925/1145261453_0.png" width="118" height="148"><div class="cover"><a class="text" href="http://www.jb51.net"><strong>yuna</strong><p>love</p></a><strong class="t g">want to know</strong><a href="/login.html" class="ppBtn"><strong class="text">YES</strong></a></div></li></ul></div>'; 
 
$obj = new HtmlAttributeFilter(); 
 
// 允许id属性 
$obj->setAllow(array('id')); 
 
$obj->setException(array( 
  'a' => array('href'),  // a 标签允许有 href属性特例 
  'ul' => array('class') // ul 标签允许有 class属性特例 
)); 
 
// img 标签忽略,不过滤任何属性 
$obj->setIgnore(array('img')); 
 
echo 'source str:<br>'; 
echo htmlspecialchars($str).'<br><br>'; 
echo 'filter str:<br>'; 
echo htmlspecialchars($obj->strip($str)); 
?> 

本文完整源码点击此处本机下载

希望本文所述对大家的PHP程序设计有所帮助。

精彩图集

赞助商链接