PHP regular expression to extract hyperlinks and their titles_PHP tutorial

WBOY
Release: 2016-07-21 14:57:41
Original
1197 people have browsed it

There is such a piece of HTML, which is quite irregular. If you want to extract the link address and link name, how do you do it?

';
The following is the quoted content:';
以下为引用的内容:
//HTML
$str = '歌曲列表
中文金曲榜
轻音乐
//HTML
$str = 'Song List
Chinese Golden Melody Chart
• Light music


Using regular expressions is the simplest. I’d like to think of other methods. . .
以下为引用的内容:
$pat = '/(.*?)/i';
preg_match_all($pat, $str, $m);
The following is the quoted content:
$pat = '/(.*?)/i';
preg_match_all($pat, $str, $m);


Output method:
以下为引用的内容:
print_r($m[2]);
print_r($m[4]);

The following is the quoted content:
print_r($m[2]) ;
print_r($m[4]);
以下为引用的内容:
for($i=0;$iecho '
  • '.$m[4][$i].'';
    }

  • or:
    The following is the quoted content:
    for($i=0;$ iecho '
  • '.$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] => 轻音乐 )
    }


  • The displayed result is:
    The following is the quoted content:

    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] => Song List [1] => Chinese Golden Song Chart [2] => Light Music)

    http://www.bkjia.com/PHPjc/363975.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/363975.html
    TechArticle
    There is such a piece of HTML, which is quite irregular. If you want to extract the link address and link name, how do you do it? The following is the quoted content: //HTML $str = 'a id=top8 href=http://list.m...
    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