Need help with PHP HTML DOM Parser

blackjack

Regular Member
Joined
Nov 23, 2007
Messages
276
Reaction score
53
I am trying to figure a way to find all links on page with certain keywords in url.

I found this which finds all links but I only want links if certain keywords in within url i.e. find all links with sales or discount in url

Code:
// Find all "A" tags and print their HREFs
foreach($html->find('a') as $e) 
    echo $e->href

So I want to do is $html->find('a' if url has sales or discount in it)

Thanks for your help
 
This should work.

Code:
// Find all "A" tags and print their HREFs
foreach($html->find('a') as $e) {
if (strpos($e,'sales') !== false || strpos($e,'discount') !== false) {
echo $e->href;
}
}
 
I would a little bit modify BlingFiles code adding non case sensitive comparison:

Code:
<?
// Find all "A" tags and print their HREFs
$words = array();
array_push($words, "sales", "discount");






foreach($html->find('a') as $e) {
    foreach($words as $word){
        if(stripos($e->href, $word) !== FALSE){
            echo $e->href;        
            break;
        }
    }
}
 
Back
Top