How to extract all links of any web page using PHP

0 193

IF You want to extract links of any web page using php script and store in your database or simply create url extractor to analyse any website then in this tutorial i am going to share simple php function which help you to extract all links of any web page, you only need to pass complete url of that website page which links you want to extract and it’ll return all the internal and external links of given web page in array form.

Here is the PHP function to extract url’s/links of given web page.

function getAllLinks($url) {
$urlData = file_get_contents($url);
$dom = new DOMDocument();
@$dom->loadHTML($urlData);
$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');
    $url = filter_var($url, FILTER_SANITIZE_URL);
    if(!filter_var($url, FILTER_VALIDATE_URL) === false){
        $urlList[] = $url;  
    }
 }
return array_unique($urlList);
}

 

Now pass URL of any web page which links you are going to extract and it’ll return all the links of that web page in array form.

$url = 'http://iamrohit.in/rohit-kumar';
var_dump(getAllLinks($url));

Using above function i have have created simple online link extractor tool. you can use above function for different purposes as per your project need.

See live demo and download source code.

See Online DEMO

Leave A Reply

Your email address will not be published.