提取HTML标签

时间:2021-05-18

<?php
/*********************************
*
*作者: 徐祖宁 (唠叨)
*邮箱: czjsz_ah@stats.gov.cn
*开发: 2002.07
*
*
*函数: tags
*功能: 从文件中提取HTML标签
*
*入口:
*$filename 文件名
*$tag标签名
*返回:
*数组,每项为:
*tagNameString
*TextString
*AttrsArray
*
*示例:
*print_r(tags("test1.htm","a"));
*print_r("http://localhost/index.htm","img");
*
*/

function tags($filename,$tag) {
$buffer = join("",file($filename));
$buffer = eregi_replace("\r\n","",$buffer);
$tagkey = sql_regcase($tag);
$buffer = eregi_replace("<$tagkey ","\n<$tag ",$buffer);
$ar = split("\n",$buffer);

foreach($ar as $v) {
if(! eregi("<$tagkey ",$v)) continue;
eregi("<$tagkey ([^>]*)((.*)</$tagkey)?",$v,$regs);
$p[tagName] = strtoupper($tag);
if($regs[3])
$p[Text] = $regs[3];
$s = trim(eregi_replace("[ \t]+"," ",$regs[1]))." ";
$s = eregi_replace(" *= *","=",$s);

$a = split(" ",$s);
for($i=0;$i<count($a);$i++) {
$ch = array();
if(eregi("=[\"']",$a[$i])) {
$j = $i+1;
while(!eregi("[\"']$",$a[$i])) {
$a[$i] .= " ".$a[$j];
unset($a[$j]);
}
}
}
foreach($a as $k) {
$name = strtoupper(strtok($k,"="));
$value = strtok("\0");
if(eregi("^[\"']",$value))
$value = substr($value,1,-1);
if($name)
$p[Attrs][$name] = $value;
}
$pp[] = $p;
}
return $pp;
}

?>

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章