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

[复制链接]

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册用户

x
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "h-t-t-p去掉-://w-w-w。去掉-更换。w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="h-t-t-p去掉-://w-w-w。去掉-更换。w3.org/1999/xhtml">
<head>
<meta h-t-t-p去掉--equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<?php
$str ='<a id="top8" href="h-t-t-p去掉-://abc。C0m 替换。和0/song/A.htm" class="p14" target="_top">歌曲列表</a><br><a target="_blank" id="bp" href="h-t-t-p去掉-://bca。C0m 替换。和0/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[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]);
?>
</body>
</html>

回复

举报 使用道具

全部回帖
暂无回帖,快来参与回复吧
您需要登录后才可以回帖 登录 | 注册用户
快速回复 返回顶部 返回列表