Home > Backend Development > PHP Tutorial > PHP regular expression to extract hyperlinks and their titles, regular expression hyperlinks_PHP tutorial

PHP regular expression to extract hyperlinks and their titles, regular expression hyperlinks_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-12 08:59:42
Original
1028 people have browsed it

PHP regular expression extracts hyperlinks and their titles. Regular expression hyperlinks

There is such a piece of HTML, which is relatively irregular. If you want to extract the link address and link in it Name, how to get it?

//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);

Output method:

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

Or:
for($i=0;$i echo ''

  • ''.$m[4][$i].''< /a>'';
    }

    The displayed result is:

    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)

    So, we came up with a method to collect the title and link address of a website. . . Apply it yourself. . .

    Reprinted from: http://www.aspnetjia.com

    www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1096602.htmlTechArticlePHP regular expression extracts hyperlinks and their titles. The regular expression hyperlink has such a piece of HTML, which is relatively irregular. , if you want to extract the link address and link name, how to do it? ...
  • 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