Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Regex和PHP检测href属性的技巧_Php_Regex_Html Parsing - Fatal编程技术网

使用Regex和PHP检测href属性的技巧

使用Regex和PHP检测href属性的技巧,php,regex,html-parsing,Php,Regex,Html Parsing,给定以下html代码,我想检测链接的真实href。你看,我在div中也有一个“假”href $html = ' <a class="test">simple text</a> <div data-href="yahoo.com">yahoo in div</div> <a class="blabla" href="google.com">google</a>'; preg_match("'<a.*?href=[

给定以下html代码,我想检测链接的真实href。你看,我在div中也有一个“假”href

$html = '
<a  class="test">simple text</a>
<div data-href="yahoo.com">yahoo in div</div>
<a class="blabla"   href="google.com">google</a>';

preg_match("'<a.*?href=[\'\"](.*?)[\'\"]'si", $html, $output);
$html='1!'
';
preg_match(“”您可以尝试以下方法:

(?<=href=")(\w+)\.\w+(?=">\1[^ ])
(?\1[^])

检查:

我会尽量简化。试试看


如果你在
之后强制输入一个空格,即使是我编辑的空格,我还是会得到yahoo;)效果很好。谢谢Jordi。。
\shref="([a-z.\/:]+)"