Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,895 questions

51,826 answers

573 users

How to extract all href links from URL in PHP

1 Answer

0 votes
class DomDocumentParser {
	private $doc;

	public function __construct($url) {

		$options = array(
			'http' => array('method' => "GET", 'header' => "User-Agent: YourBotName/0.1\n")
		);
		$context = stream_context_create($options);

		$this->doc = new DomDocument();
		@$this->doc->loadHTML(file_get_contents($url, false, $context));
	}

	public function getlinks() {
		return $this->doc->getElementsByTagName("a");
	}
}

function extract_href_links($url) {
	$parser = new DomDocumentParser($url);

	$allLinks = $parser->getLinks();

	foreach ($allLinks as $link) {
		$href = $link->getAttribute("href");
		echo $href . "<br />";
	}
}

$url = "https://www.collectivesolver.com/";
extract_href_links($url);


/*
run:

./26670/how-to-check-if-a-string-is-rotation-of-another-string-in-java
./26670/how-to-check-if-a-string-is-rotation-of-another-string-in-java?show=32942#a32942
./user/avibootz
./tag/JAVA
./32936/how-to-remove-specific-char-from-string-using-stringbuilder-in-java
./32936/how-to-remove-specific-char-from-string-using-stringbuilder-in-java?show=32937#a32937
./user/avibootz
./tag/JAVA
./32934/how-to-remove-specific-char-from-string-in-java
./32934/how-to-remove-specific-char-from-string-in-java?show=32935#a32935
./user/avibootz
./tag/JAVA
./32932/how-to-convert-an-array-of-integers-to-string-in-java
./32932/how-to-convert-an-array-of-integers-to-string-in-java?show=32933#a32933
./user/avibootz
./tag/JAVA
./32930/how-to-convert-an-array-of-strings-to-string-in-java
./32930/how-to-convert-an-array-of-strings-to-string-in-java?show=32931#a32931
./user/avibootz
./tag/JAVA
./32926/how-to-stop-execution-of-a-program-in-php
./32926/how-to-stop-execution-of-a-program-in-php?show=32929#a32929
./user/avibootz
./tag/PHP
...

*/

 



answered Jul 27, 2020 by avibootz

Related questions

1 answer 153 views
1 answer 219 views
1 answer 119 views
1 answer 98 views
98 views asked Jul 16, 2024 by avibootz
2 answers 250 views
2 answers 152 views
152 views asked Aug 14, 2020 by avibootz
...