Home  >  Article  >  Backend Development  >  求教 获取如下href的正则要怎么写

求教 获取如下href的正则要怎么写

WBOY
WBOYOriginal
2016-06-23 14:23:28933browse

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>

想要用正则获取标签内的每一个的href
请问要怎么写?

回复讨论(解决方案)

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


这是先把里面的
先匹配出来,然后在匹配出href
有没有一个正则就匹配到的方法?

一个正则,我没有这个本事。
不过除了正则,还可以用dom解析,包括simplehtmldom

有没有能用一个正则就匹配出来的呢?

~\s*(]+href="(.*?)">.*?)*?\s*~msi

大体如此,自己调一下

Statement:
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