您的位置:首页 > 编程语言 > PHP开发

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

2014-12-19 17:01 459 查看
<?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"> <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[2]);
print_r($m[4]);
for($i=0;$i<count($m[2]) ;$i++){
echo '<li><a href="'.$_SERVER['PHP_SELF'].'?url='.$m[2][$i].'">'.$m[4][$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[1]);
print_r($m[2]);
echo "<hr />";
//iframe地址(通用)
preg_match_all('/<iframe.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>(.*?)<\/iframe.*?>/sim',$str,$m);
print_r($m[1]);
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: