PHP正则表达式匹配并输出数组(做小偷程序很有用)

来源:互联网 发布:ubuntu启动selinux 编辑:程序博客网 时间:2024/06/07 05:17

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

//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>'; copyright dedecms

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

内容来自dedecms

 

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

织梦内容管理系统

 

输出方法: dedecms.com

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>';
}

内容来自dedecms

 

显示结果是:

本文来自织梦

 

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] => 轻音乐 )

本文来自织梦

 

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

原创粉丝点击