1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Need help with PHP HTML DOM Parser

Discussion in 'PHP & Perl' started by blackjack, Nov 30, 2013.

  1. blackjack

    blackjack Regular Member

    Joined:
    Nov 23, 2007
    Messages:
    276
    Likes Received:
    51
    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
     
  2. BlingFiles

    BlingFiles Regular Member

    Joined:
    Nov 28, 2013
    Messages:
    203
    Likes Received:
    37
    Location:
    http://blingfiles.com
    Home Page:
    This should work.

    Code:
     
    • Thanks Thanks x 1
  3. tratata

    tratata Newbie

    Joined:
    Jul 26, 2013
    Messages:
    14
    Likes Received:
    5
    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;
            }
        }
    }
    
    
     
    • Thanks Thanks x 1
  4. Standard Toaster

    Standard Toaster Regular Member

    Joined:
    Aug 29, 2009
    Messages:
    335
    Likes Received:
    190
    • Thanks Thanks x 2
    Last edited: Nov 30, 2013