专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »PHP教程 » 正则表达式提取:PHP正则表达式提取超链接及其标题 »正文

正则表达式提取:PHP正则表达式提取超链接及其标题

来源: 发布时间:星期一, 2009年1月12日 浏览:152次 评论:0
  有这么段HTML比较不规则如果要提取其中链接地址和链接名称如何弄?

  //HTML

$str = '<a id="top8" href="http://list.MP3.baidu.com/song/A.htm?top8" ="p14" target="_top">歌曲列表</a><br><a target="_blank" id="bp" href="http://list.mp3.baidu.com/list/bangping.html" ="p14">中文金曲榜</a><br><td nowrap="nowrap">&#8226;&nbsp;<a id="top19" href="qingyinyue.html?top19" ="p14" target="_top">轻音乐</a></td>';

  利用正则表达式是最简单其它办法偶米去想

$pat = '/<a(.*?)href="(.*?)"(.*?)>(.*?)</a>/i';
preg_match_all($pat, $str, $m);


  输出思路方法:

pr_r($m[2]);
pr_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>';
}


  显示结果是:

Array ( [0] => http://list.mp3.baidu.com/song/A.htm?top8 [1] => http://list.mp3.baidu.com/list/bangping.html [2] => qingyinyue.html?top19 ) Array ( [0] => 歌曲列表 [1] => 中文金曲榜 [2] => 轻音乐 )

  于是我们要采集某个网站WebSite标题及链接地址思路方法就出来了自己套用吧

0

相关文章

读者评论

发表评论

  • 昵称:
  • 内容: