`
- 浏览:
214314 次
- 性别:
- 来自:
北京
-
<?php
/********************************************************************
* 原文件名:Filter1.php
* 文件说明:过滤HTML字串
* 文件编写:xuefengal@sohu.com
* 流程说明:
* 当附合要求的参数传递进filter函数后,filter()函数首先
* 把要字串中所有要过滤的标签$tag通过preg_match_all()
* 取出来,然后循环preg_match_all的匹配数组,通过preg_split()
* 函数分割每个标签为 "左边属性" = "右边值"的形式,再从要保
* 留的属性数组中循环,将preg_split()匹配的内容对应取出,构成
* 可以替换的值,后最通过str_replcae()替换掉字串中相应的标签
* 函数列表:
* function filter(&$str,$tag,$keep_attribute)
* function match($reg,&$str,$arr)
* function show($str,$title='',$debug = True)
* 使用示例:
* //取得搜狐新闻首页
* $str = @file_get_content("http://news.sohu.com");
* //过滤
* filter($str,'a','href,target,alt');
* filter($str,'p','align');
* show($str,'过滤后的内容');
********************************************************************/
$start_time = array_sum(explode(" ",microtime()));
$str = <<< HTML
<A style="a" target=_blank href='http://www.a.com' xxx=xadsfa alt="a a a" style="aa">site a</A>
<A alt='b b b' xxx=xadsfa target=_blank href='http://www.b.com' style="b" style="bb">site b</A>
<A xxx=xadsfa style="c" href='http://www.c.com' target=_blank alt=c c c style="cc">site c</A>
<A style="d" href='http://www.d.com' xxx=xadsfa alt=d d d target=_blank style="dd">site d</A>
<A target=_blank style="e" xxx=xadsfa style="ee" alt=e e e href='http://www.e.com'>site e</A>
<p align=right style="font-size:10px">adasdfasdf</p>
<p style="font-color:red;" align='left'>asdfasdfasdfasdf</p>
<p align=left right center>asdfasdfasdf</p>
<font color="red" alt=adasd adsasd>asdfadsfasdf</font>
<font align='left' color=red>asdfasdfadf</font>
<font align=left right color=red black>asdfasdf</font>
HTML;
//显示原字串
show($str,'Html');
/***********************************************************************************************************************************************************************/
//过滤
filter($str,'a','href,target,alt');
filter($str,'p','align');
filter($str,'font','color,alt');
//显示过滤后的内容
show($str,'Result');
//脚本运行时间
$run_time = array_sum(explode(" ",microtime())) - $start_time;
echo('<center>Script Run Time: '.$run_time.'</center>');
/**
* 说明:过滤HTML字串
* 参数:
* $str : 要过滤的HTML字串
* $tag : 过滤的标签类型
* $keep_attribute :
* 要保留的属性,此参数形式可为
* href
* href,target,alt
* array('href','target','alt')
*/
function filter(&$str,$tag,$keep_attribute) {
//检查要保留的属性的参数传递方式
if(!is_array($keep_attribute)) {
//没有传递数组进来时判断参数是否包含,号
if(strpos($keep_attribute,',')) {
//包含,号时,切分参数串为数组
$keep_attribute = explode(',',$keep_attribute);
}else {
//纯字串,构造数组
$keep_attribute = array($keep_attribute);
}
}
echo("·过滤[$tag]标签,保留属性:".implode(',',$keep_attribute).'<br>');
//取得所有要处理的标记
$pattern = "/<$tag(.*)<//$tag>/i";
preg_match_all($pattern,$str,$out);
//循环处理每个标记
foreach($out[1] as $key => $val) {
//取得a标记中有几个=
$cnt = preg_split('/ *=/i',$val);
$cnt = count($cnt) -1;
//构造匹配正则
$pattern = '';
for($i=1; $i<=$cnt; $i++) {
$pattern .= '( .*=.*)';
}
//完成正则表达式形成,如/(<a)( .*=.*)( .*=.*)(>.*<//a>/i的样式
$pattern = "/(<$tag)$pattern(>.*<//$tag>)/i";
//取得保留属性
$replacement = match($pattern,$out[0][$key],$keep_attribute);
//替换
$str = str_replace($out[0][$key],$replacement,$str);
}
}
/**
* 说明:构造标签,保留要保留的属性
* 参数:$reg : pattern,preg_match的表达式
* $str : string,html字串
* $arr : array,要保留的属性
* 返回:
* 返回经保留处理后的标签,如
* <A href='http://www.e.com' target=_blank alt=e e e>e.com</A>
*/
function match($reg,&$str,$arr) {
//match
preg_match($reg,$str,$out);
//取出保留的属性
$keep_attribute = '';
foreach($arr as $k1=>$v1) {
//定义的要保留的属性的数组
foreach($out as $k2=>$v2) {
//匹配=后的数组
$attribute = trim(substr($v2,0,strpos($v2,'=')));
//=前面的
if($v1 == $attribute) {
//要保留的属性和匹配的值的=前的部分相同
$keep_attribute .= $v2;
//保存此匹配部分的值
}
}
}
//构造返回值,结构如:<a href=xxx target=xxx class=xxx>aadd</a>
$keep_attribute = $out[1].$keep_attribute.($out[count($out)-1]);
//返回值
Return $keep_attribute;
}
/**
* 显示字串内容
*/
function show($str,$title='',$debug = True) {
if($debug) {
if(is_array($str)) {
$str = print_r($str,True);
}
$txtRows = count(explode("/n",$str))+1;
echo($title.':<br><TEXTAREA NAME="txt" ROWS="'.$txtRows.'" COLS="130">'.$str.'</TEXTAREA><br><br>');
}
}
?>
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
html转C语言字串或数组工具,还可以转图片。是C语言开发网页的好助手。
字串替换器 1.05 版
android字串的拆分问题
点睛字串替换器
Java 基本语法(四)——字串和数组 Java 基本语法(四)——字串和数组
常用的字串函数,比Delphi自带的速度快很多! StrPos,StrRplc,及其它常用,易用性函数。
导出android代码内的字串到excel,便于翻译。 导出后的格式是: key 英文 待翻译的语言 导出时,会自动忽略不必要的字串。比如注释里带not translate,或者属性里有translate="false"的字串。 ------------ ./string...
撰写应用程式时,常会将程式设定值储存成文字档(*.ini, *.config, *.xml),一般比较资安敏感的字串(如资料库连线字串)写在这些没有加密的文字档里,容易被人窥看。兼顾简易及可逆的解密需求,我们会在文档中替代成已...
big-list-of-naughty-strings-淘气字串大列表 ~ 收集了用户输入时经常出错的字串, 包含 py 脚本和 JSON 格式数据
数据库字串批量替换工具,绿色免安装,简单实用~
OLLYDBG是一个新的动态追踪工具,将IDA与SoftICE结合起来的思想,Ring 3级调试器,非常容易上手,己代替SoftICE成为当今最为流行的调试解密工具了。同时还支持插件扩展功能,是目前最强大的调试工具。
主要介绍了PHP中实现中文字串截取无乱码的解决方法,直接使用PHP函数substr截取中文字符可能会出现乱码,下面跟随脚本之家小编一起看看具体解决方法吧
字串截取.c
查找相关类型文件,找出含有相关字串的所有文件
2050:【例5.20】字串包含 时间限制: 1000 ms 内存限制: 65536 KB 提交数: 19468 通过数: 8142 【题目描述】 字符串移位包含问题。 对于一个字符串来说,定义一次循环移位操作为:将字符串的第一个字符移动到末尾...
逆向分析基础 OllyDBG 入门系列(二)-字串参考
精确字串匹配算法手册,内含30种字串匹配算法分析及实现
bfs 洛谷 字串变化AC代码 字串变化.cpp
相信有很多朋友被黑客把数据库里的数据后面都加了病毒代码和JS代码吧, 不要急,本人也是受害者,使用我这个工具就行啦!也不用写复杂的SQL语句去替换。操作简单。
C++11学习笔记 - 字串数据类型希望你觉得有用