2015-01-17 21 views
-1

我想獲得那些屬性爲data-gaevent="people"的鏈接值 在這種情況下,這是第一個鏈接。使用specifice屬性在PHP RegExp中獲取href值

<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="(.*)" 

我怎麼能在正則表達式

+2

更好地利用DOM比正則表達式 – anubhava

回答

-1

規定的條件。假定數據gaevent總是在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];