Home >Backend Development >PHP Tutorial >PHP gets all links in a page

PHP gets all links in a page

WBOY
WBOYOriginal
2016-07-25 08:45:20954browse
By using this code snippet, you can easily extract all links on any web page.
  1. $html = file_get_contents('http://www.example.com');
  2. $dom = new DOMDocument();
  3. @$dom->loadHTML($html);
  4. // grab all the on the page
  5. $xpath = new DOMXPath($dom);
  6. $hrefs = $xpath->evaluate("/html/body//a");
  7. for ($i = 0; $i < ; $hrefs->length; $i++) {
  8. $href = $hrefs->item($i);
  9. $url = $href->getAttribute('href');
  10. echo $url.'
  11. ';
  12. }
Copy code

PHP


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn