Skip to content

Get ALL Links From Page with php ?

This is post is about getting All Links from the given URL, we can achieve same Output with various methods but i am gonna use DOMDocument Class, DOMDocument class is PHP built in Library, it Represents an entire HTML or XML document & serves as the root of the document tree.

Here is the Example Script ,
If you’re opening a URI with special characters, such as spaces, you need to encode the URI with urlencode().

loadHTML($html);
 
$xpath = new DOMXPath($dom_object);
$hrefs = $xpath->evaluate("/html/body//a");
 
for ($i = 0; $i < $hrefs->length; $i++) {
       $href = $hrefs->item($i);
       $url = $href->getAttribute('href');
       echo $url.'
'; } ?>

PHP Functions

file_get_contents() : Reads entire file into a string
urlencode() : URL-encodes string

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments