32rwqe 发表于 2014-12-22 08:54:08

PHP正则获取A标签的链接地址

<?php
$str ='<a id="top8" href="http://abc.com/song/A.htm" class="p14" target="_top">歌曲列表</a><br><a target="_blank" id="bp" href="http://bca.com/list/bangping.html" class="p14">中文金曲榜</a><br><td nowrap="nowrap">&nbsp;<a id="top19" href="qingyinyue.html" class="p14" target="_top">轻音乐</a></td>';
$str = $str ."<iframe src=\"/info/public/bipin.shtml\" id=\"leitai\" name=\"leitai\" frameborder=\"0\" scrolling=\"no\" width=\"100%\" height=\"307px;\"></iframe>";
//链接地址+标题(href必须带双引号)
$pat ='/<a(.*?)href="(.*?)"(.*?)>(.*?)<\/a>/i';
preg_match_all($pat, $str, $m);
print_r($m);
print_r($m);
for($i=0;$i<count($m) ;$i++){
   echo '<li><a href="'.$_SERVER['PHP_SELF'].'?url='.$m[$i].'">'.$m[$i].'</a></li>';
}
echo "<hr />";
//仅链接地址(href必须带双引号)
preg_match_all('/(?<=href=")[\w\d\.:\/]*/',$str,$m);
print_r($m);
echo "<hr />";
//链接地址+标题(通用)
preg_match_all('/<a.*?(?: |\\t|\\r|\\n)?href=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>(.+?)<\/a.*?>/sim',$str,$m);
print_r($m);
print_r($m);
echo "<hr />";
//iframe地址(通用)
preg_match_all('/<iframe.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>(.*?)<\/iframe.*?>/sim',$str,$m);
print_r($m);
?>

页: [1]
查看完整版本: PHP正则获取A标签的链接地址