Jump to content

[SOLVED] catch the hyperlinks by class attribute


msandal

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 />';
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.