使用Regex和PHP检测href属性的技巧
给定以下html代码,我想检测链接的真实href。你看,我在div中也有一个“假”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 = '
<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.\/:]+)"