使用 specifice 属性获取 PHP RegExp 中的 href 值


Get href value in PHP RegExp using specifice attribute

我想获取属性为data-gaevent="people"的链接值 在本例中,这是第一个链接。

<a class="clickstream-link" data-clickstream-record-number="0" data-gaaction="people_serp_0" data-gaevent="people" data-galabel="click" href="/name/Raymundo-Mario-J/Santa-Ana-CA/c7z9sd9" itemprop="url" rel="nofollow">  

<a class="clickstream-link" data-clickstream-record-number="0" data-gaaction="people_serp_0" data-gaevent="anyother" data-galabel="click" href="/name/Raymundo-Mario-J/Santa-Ana-CA/c7z9sd9" itemprop="url" rel="nofollow">  

这是正则表达式:

<a.*href="(.*)"

如何在正则表达式中设置条件

假设数据事件总是在 href 之前...

$url = '<a class="clickstream-link" data-clickstream-record-number="0" data-gaaction="people_serp_0" data-gaevent="people" data-galabel="click" href="/name/Raymundo-Mario-J/Santa-Ana-CA/c7z9sd9" itemprop="url" rel="nofollow">';
preg_match('~<a.*?data-gaevent="people".*?'s+href="(.*?)".*~', $url, $link);
echo $link[1];