Home >Backend Development >PHP Tutorial >PHP gets all links in the specified URL page

PHP gets all links in the specified URL page

WBOY
WBOYOriginal
2016-07-25 08:42:261306browse
  1. //获取指定URL页面中所有链接
  2. function get_url_href($url){
  3. $html = file_get_contents($url);
  4. $dom = new DOMDocument();
  5. @$dom->loadHTML($html);
  6. $xpath = new DOMXPath($dom);
  7. $hrefs = $xpath->evaluate('/html/body//a');
  8. for($i=0;$i<$hrefs->length;$i++){
  9. $href = $hrefs->item($i);
  10. $url = $href->getAttribute('href');
  11. if(substr($url,0,4) == 'http') echo $url.'
    ';
  12. }
  13. }
复制代码

PHP, URl


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
Previous article:php date processing codeNext article:php date processing code