PHP gets all links in a page

WBOY
Release: 2016-07-25 08:45:20
Original
901 people have browsed it
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


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template