带有 PHP 模式的正则表达式


regex with php pattern

<h3 class="r"><a href="/url?sa=t&amp;rct=j&amp;q=&amp;esrc=s&amp;source=web&amp;cd=1&amp;cad=rja&amp;uact=8&amp;ved=0CCUQFjAA&amp;url=http%3A%2F%2Fstackoverflow.com%2Fquestions%2F18682352%2Fgoogle-search-results-with-php&amp;ei=9ptsVNivF_iTsQTv-YJ4&amp;usg=AFQjCNFdoi58ua_4oBtPM4LHybHRZVF9jQ&amp;bvm=bv.80120444,d.cWc" onmousedown="return rwt(this,'','','','1','AFQjCNFdoi58ua_4oBtPM4LHybHRZVF9jQ','','0CCUQFjAA','','',event)" data-href="http://stackoverflow.com/questions/18682352/google-search-results-with-php">curl - Google search results with php - Stack Overflow</a></h3>

请提供数据 href 值正则表达式模式。

可以尝试使用此模式

$str = '<h3 class="r"><a href="/url?sa=t&amp;rct=j&amp;q=&amp;esrc=s&amp;source=web&amp;cd=1&amp;cad=rja&amp;uact=8&amp;ved=0CCUQFjAA&amp;url=http%3A%2F%2Fstackoverflow.com%2Fquestions%2F18682352%2Fgoogle-search-results-with-php&amp;ei=9ptsVNivF_iTsQTv-YJ4&amp;usg=AFQjCNFdoi58ua_4oBtPM4LHybHRZVF9jQ&amp;bvm=bv.80120444,d.cWc" onmousedown="return rwt(this,'''','''','''',''1'',''AFQjCNFdoi58ua_4oBtPM4LHybHRZVF9jQ'','''',''0CCUQFjAA'','''','''',event)" data-href="http://stackoverflow.com/questions/18682352/google-search-results-with-php">curl - Google search results with php - Stack Overflow</a></h3>';
$re = '/data-href=["'']?([^"'' ]*)["'' ]/is';
preg_match($re, $str, $m);
$attr_value = trim(str_replace('data-href=', '', $m[0]), '"');
echo $attr_value;

输出:

http://stackoverflow.com/questions/18682352/google-search-results-with-php