msandal Posted August 23, 2008 Share Posted August 23, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/121026-solved-catch-the-hyperlinks-by-class-attribute/ Share on other sites More sharing options...
Zane Posted August 23, 2008 Share Posted August 23, 2008 /href=["\']([^"\']+)["\'].*class=["\']link["\']/ Quote Link to comment https://forums.phpfreaks.com/topic/121026-solved-catch-the-hyperlinks-by-class-attribute/#findComment-623921 Share on other sites More sharing options...
nrg_alpha Posted August 24, 2008 Share Posted August 24, 2008 $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 />'; } Quote Link to comment https://forums.phpfreaks.com/topic/121026-solved-catch-the-hyperlinks-by-class-attribute/#findComment-624017 Share on other sites More sharing options...
msandal Posted August 24, 2008 Author Share Posted August 24, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/121026-solved-catch-the-hyperlinks-by-class-attribute/#findComment-624147 Share on other sites More sharing options...
nrg_alpha Posted August 24, 2008 Share Posted August 24, 2008 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 />'; } Quote Link to comment https://forums.phpfreaks.com/topic/121026-solved-catch-the-hyperlinks-by-class-attribute/#findComment-624331 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.