Jump to content

[SOLVED] catch the hyperlinks by class attribute


Recommended Posts

Hi.

I need to catch the address in the hyperlinks. But, the hyperlinks must have the class="link" property.

 

sample html source:

<a href="www.sample1.com" class="link" onmousedown="return click();">sample text 1</a>
<a href="www.sample2.com" onmousedown="return click();">sample text 2</a>
<a href="www.sample3.com" class="link" onmousedown="return click();">sample text 3</a>
<a href="www.sample4.com" onmousedown="return click();">sample text 4</a>

 

result must be like this:

www.sample1.com
www.sample3.com

 

Can you help me?

Best regards.

$str = '<a href="www.sample1.com" class="link" onmousedown="return click();">sample text 1</a>
        <a href="www.sample2.com" onmousedown="return click();">sample text 2</a>
        <a href="www.sample3.com" class="link" onmousedown="return click();">sample text 3</a>
        <a href="www.sample4.com" onmousedown="return click();">sample text 4</a>';

preg_match_all('#href=["\'](.+)["\']\s?class=["\']link["\']#i', $str, $match);
foreach($match[1] as $val){
   echo $val . '<br />';
}

thank you for replies. nonetheless, there is a small problem.

 

my html source are the single line. like this:

 

<a href="www.sample1.com" class="link" onmousedown="return click();">sample text 1</a><a href="www.sample2.com" onmousedown="return click();">sample text 2</a><a href="www.sample3.com" class="link" onmousedown="return click();">sample text 3</a><a href="www.sample4.com" onmousedown="return click();">sample text 4</a>

 

What must I do in thus state?

 

 

note:

with this method, I try to catch the google links in the result page.

Ah, well.. In your first post, you have those URLS listed as a stack.. if had made it one line to begin with, it would have been much clearer (for future refernce, try to present and declare things EXACTLY as is.. just makes for less confusion down the road)...

 

$str = '<a href="www.sample1.com" class="link" onmousedown="return click();">sample text 1</a><a href="www.sample2.com" onmousedown="return click();">sample text 2</a><a href="www.sample3.com" class="link" onmousedown="return click();">sample text 3</a><a href="www.sample4.com" onmousedown="return click();">sample text 4</a>';
preg_match_all('#href=["\']([-\w.]+)["\']\s?class=["\']link["\']#i', $str, $matches);
foreach($matches[1] as $val){
   echo $val . '<br />';
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.