Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all link from a html

In PHP how can I get all link from a HTML document.

For example: <a href="www.blabla.com">To Bla</a>

In php I need the link: www.blabla.com.

Thanks for your helps.

like image 469
OHLÁLÁ Avatar asked Aug 25 '26 21:08

OHLÁLÁ


1 Answers

Maybe this will help: http://www.the-art-of-web.com/php/parse-links/

Summary:

<?php

// Original PHP code by Chirp Internet: www.chirp.com.au
// Please acknowledge use of this code by including this header.

$url = "http://www.example.net/somepage.html";
$input = @file_get_contents($url) or die("Could not access file: $url");
$regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>";
if(preg_match_all("/$regexp/siU", $input, $matches)) {
    // $matches[2] = array of link addresses
    // $matches[3] = array of link text - including HTML code
}

?>
like image 189
Ben Novakovic Avatar answered Aug 27 '26 17:08

Ben Novakovic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!