php's method of finding all the links on any page

  • 2020-11-20 06:02:21
  • OfStack

With DOM, you can easily grab links from any page, as shown in the following code:
 
$html = file_get_contents('http://www.example.com'); 
$dom = new DOMDocument(); 
@$dom->loadHTML($html); 
// grab all the on the page 
$xpath = new DOMXPath($dom); 
$hrefs = $xpath->evaluate("/html/body//a"); 
for ($i = 0; $i < $hrefs->length; $i++) { 
$href = $hrefs->item($i); 
$url = $href->getAttribute('href'); 
echo $url.'<br />'; 
} 

Related articles: