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


有这么一段HTML,比较不规则的,如果要提取其中的链接地址和链接名称,怎么弄?

  //HTML

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

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

$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="'. PHP正则表达式提取超链接及其标题 - 站长学院

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

2009-3-19 0:26:00查看学习心得
{GetProperty(Content)}
« 
» 

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3