Posted By

Unreal on 02/28/09


Tagged

page DOM links a all find on the


Versions (?)

Who likes this?

5 people have marked this snippet as a favorite

Bluewall
gdvickery
vali29
jodm
BrianCoyDesign


Find All The Links On A Page


 / Published in: PHP
 

URL: http://www.imben.co.uk

The basis to a spider.

  1. $html = file_get_contents('http://www.example.com');
  2.  
  3. $dom = new DOMDocument();
  4. @$dom->loadHTML($html);
  5.  
  6. // grab all the on the page
  7. $xpath = new DOMXPath($dom);
  8. $hrefs = $xpath->evaluate("/html/body//a");
  9.  
  10.  
  11.  
  12. for ($i = 0; $i < $hrefs->length; $i++) {
  13. $href = $hrefs->item($i);
  14. $url = $href->getAttribute('href');
  15.  
  16. echo $url.'<br />';
  17. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: hassancent on April 21, 2013

Thanks man. You solved my problem.

You need to login to post a comment.