Home >Backend Development >PHP Tutorial >PHP regular expression to extract hyperlinks and their titles, regular expression hyperlinks_PHP tutorial
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
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
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