使用特定属性获取 PHP RegEx 中的 href 值
Get href value in PHP RegExp using specifice attribute
我想获取属性为data-gaevent="people"
的那些link值
在这种情况下,这是第一个 link。
<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="(.*)"
如何在 RegEXP 中设置条件
假设 data-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];
我想获取属性为data-gaevent="people"
的那些link值
在这种情况下,这是第一个 link。
<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="(.*)"
如何在 RegEXP 中设置条件
假设 data-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];