Hi everybody. In this article I will show you how to get all the links from an URL. I have used simple DOM concept to find the html data from URL. file_get_contents and loadHTML is used to retrieve the html for URL and DOMXPath to grab it. Find this code snippet below.
Hope that this article will help you
PHP code below
//URL
$srcUrl='http://www.webinfopedia.com/';
$html = file_get_contents($srcUrl);
$dom = new DOMDocument();
@$dom->loadHTML($html);
// grab all the on the page
$xpath = new DOMXPath($dom);
//finding the a tag
$hrefs = $xpath->evaluate("/html/body//a");
//Loop to display all the links
for ($i = 0; $i < $hrefs->length; $i++) {
$href = $hrefs->item($i);
$url = $href->getAttribute('href');
//Filter the null links
if($url!='#')
{
echo "".$url."
";
}
}
Hope that this article will help you