Home > Backend Development > PHP Tutorial > PHP正则表达式提取超链接及其标题

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

WBOY
Release: 2016-06-23 13:17:38
Original
1016 people have browsed it

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

//HTML
$str = ''歌曲列表
中文金曲榜

• 轻音乐 '';

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

$pat = ''/(.*?)/i'';
preg_match_all($pat, $str, $m);

输出方法:

print_r($m[2]);
print_r($m[4]);

或者:
for($i=0;$i    echo ''

  • ''.$m[4][$i].'''';
    }

    显示结果是:

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

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

    转载自:http://www.aspnetjia.com

  • source:php.cn
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template