Find All Links on a Page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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 />'; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Using the DOM, you can easily grab all links from any webpage. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment