How to get string between divs

Discussion in 'PHP' started by Cooker, May 5, 2009.

  1. #1
    Hi!

    I want to writa a script saving links from one site.
    There are over 30 link in one page, the have got the same divs.
    What php function should I use to get the links ?
    Thanks.
     
    Cooker, May 5, 2009 IP
  2. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #2
    Hi,

    May be you have to use preg_match but if you place here input string and what output string have to be will be much better.

    ps: This thread is similar.
    Regards
     
    koko5, May 5, 2009 IP
  3. shpshftr

    shpshftr Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi,

    if you're unfamiliar with regular expressions, you could do it another way (a bit cranky): use strip_tags function to get page code without tag text, then use explode function with blank space argument to break page code into separate words and then strpos to determine which of words have "http://" prefix.

    Regards
     
    shpshftr, May 5, 2009 IP
  4. JDevereux

    JDevereux Peon

    Messages:
    50
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    // I use curl but file_get_contents will work for most domains
    $html = file_get_contents('http://news.google.com/');
    
    $dom = new DOMDocument();
    
      @$dom->loadHTML($html);
            
      $links = $dom -> getElementsByTagName('a');
    
      foreach ($links as $link) {
        $anchor_text = $link -> firstChild -> data;
        $href = $link ->getAttribute('href'); 
        echo '<a href="' . $href . '">' . $anchor_text . '</a><br />';    
        }
    PHP:
     
    JDevereux, May 5, 2009 IP
  5. Cooker

    Cooker Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    so, input string is html code... output should be links having specified endings:)
     
    Cooker, May 6, 2009 IP
  6. Aaron Sustar

    Aaron Sustar Peon

    Messages:
    38
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    If I get this right, you could make some changes to JDevereux excellent code:

    
    $specified_ending = ".htm";   // this is the specified ending you mentioned, change it at will
    $dom = new DOMDocument();
    @$dom->loadHTML($html);   // $html is your input HTML code 
    $links = $dom->getElementsByTagName('a');
    $wanted_links = array();
    foreach ($links as $link) {
        $href = $link->getAttribute('href');
        if (strpos($href, $specified_ending) == strlen($href) - strlen($specified_ending)) {
                $wanted_links[] = $href;
        }
    }
    PHP:
    This way you can specify the desired ending of links and all appropriate links are stored in an array $wanted_links.
     
    Aaron Sustar, May 7, 2009 IP