Maison > développement back-end > tutoriel php > 求教 获取如下href的正则要怎么写

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Libérer: 2016-06-23 14:23:28
original
976 Les gens l'ont consulté

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>
Copier après la connexion

想要用正则获取标签内的每一个的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);
Copier après la connexion
Copier après la connexion

$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);
Copier après la connexion
Copier après la connexion


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

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

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

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

大体如此,自己调一下

Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal