Maison > Article > développement back-end > 求教 获取如下href的正则要怎么写
php 正则
test.html<!DOCTYPE html><html> <head>test</head> <body> <a id="a5" href="111">wawa</a> <a id="a6" href="222">hehe</a> <a id="a7" href="333">haha</a> <a id="a8" href="444">uuuu</a> ...... <b> <a id="a1" href="111">wawa</a> <a id="a2" href="222">hehe</a> <a id="a3" href="333">haha</a> <a id="a4" href="444">uuuu</a> </b> ........ </body></html>
$html = <<<TTT<!DOCTYPE html><html> <head>test</head> <body> <a id="a5" href="111">wawa</a> <a id="a6" href="222">hehe</a> <a id="a7" href="333">haha</a> <a id="a8" href="444">uuuu</a> ...... <b> <a id="a1" href="111">wawa</a> <a id="a2" href="222">hehe</a> <a id="a3" href="333">haha</a> <a id="a4" href="444">uuuu</a> </b> ........ </body></html>TTT;preg_match_all('@<b>([^`]*?)<\/b>@i',$html,$match);preg_match_all('@href="(.+?)"@i',$match[0][0],$matches);var_dump($matches);
$html = <<<TTT<!DOCTYPE html><html> <head>test</head> <body> <a id="a5" href="111">wawa</a> <a id="a6" href="222">hehe</a> <a id="a7" href="333">haha</a> <a id="a8" href="444">uuuu</a> ...... <b> <a id="a1" href="111">wawa</a> <a id="a2" href="222">hehe</a> <a id="a3" href="333">haha</a> <a id="a4" href="444">uuuu</a> </b> ........ </body></html>TTT;preg_match_all('@<b>([^`]*?)<\/b>@i',$html,$match);preg_match_all('@href="(.+?)"@i',$match[0][0],$matches);var_dump($matches);
一个正则,我没有这个本事。
不过除了正则,还可以用dom解析,包括simplehtmldom
有没有能用一个正则就匹配出来的呢?
~\s*(]+href="(.*?)">.*?)*?\s*~msi
大体如此,自己调一下