Find All The Links On A Page


/ Published in: PHP

The basis to a spider.


Copy this code and paste it in your HTML
  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. }

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.