Have a Snippet?

Keep, track and share your code snippets with your friends

PHP: Find all the links on a page Share on Vkontakte

Find all the links on a page (except of dummy links like "#")

$html = file_get_contents('https://snippets.pro');
$dom = new DOMDocument();
@$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$links = $xpath->evaluate("/html/body//a");

for ($i = 0; $i < $links->length; $i++) {
    $link = $links->item($i);
    $url = $link->getAttribute('href');
    if (strpos($url, '#') === false)
        echo $url . '
'; }


Tag: PHP, Links, List

0 Comments