欢迎您访问我爱IT技术网,今天小编为你分享的编程技术是:【PHP常用函数:过滤HTML字符串】,下面是详细的分享!
PHP常用函数:过滤HTML字符串
$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>');
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);
}
}
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=http://www.chinaz.com/program/2010/0304/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>');
}
}
?>
以上所分享的是关于PHP常用函数:过滤HTML字符串,下面是编辑为你推荐的有价值的用户互动:
相关问题:php过滤html php标签
答:用PHP的strip_tags()函数,参考文档:http://www.w3school.com.cn/php/func_string_strip_tags.asp >>详细
相关问题:求一个简单的php语句,截取字符,过滤html代码。
答:去除html标签 function strip_tags($string, $replace_with_space = true) { if ($replace_with_space) { return preg_replace('!]*?>!', ' ', $string); } else { return strip_tags($string); } } 截取字符函数(匹配各种编码) function trun... >>详细
相关问题:请问用PHP如何过滤空格?内置函数或者正则表达式都...
答:你是头尾空格吗? 用trim函数 如果是其他位置用str_replace(" ","","$array"); str_replace(find,replace,string,count) 参数 描述 find 必需。规定要查找的值。 replace 必需。规定替换 find 中的值的值。 string 必需。规定被搜索的字符串。 c... >>详细
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
