Jump to content

Preg_Mtach_All: URL Regex Help Need


seb hughes

Recommended Posts

I'm written a script which grabs all URLs from a page and outputs them and also it takes out all duplicates. I need it to grab from a <a href ="www.domain.com>akhajha</a> just the www.domain.com or domain.net. If the link is www.domain.org/helllloooooo.html it needs to trim itto www.domain.org. I written the program, just this Regex is driving me nuts.

 

	$url = $_POST['url'];
$banned = array($url);
$handle = fopen($url, "r"); 
$html = file_get_contents($url); 
$matches = array();
$status = preg_match_all('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', $html, $matches);
$unique = array();

	foreach($matches[1] as $match) {
		$found = false;
			if (in_array($match, $banned)) {
				$found = true;
			}

	foreach($unique as $u) {
		if($u == $match) {
			$found = true;
			break;
		}
	}
	if(!$found) {
	array_push($unique, $match);
	}
}
	foreach ($unique as $link) {
	echo $link . "<br />";
}

 

Please help me fix this problem. Thanks.

Link to comment
Share on other sites

Is "www." always a requirement? There are "ww2."s I think, along with a variety of domain endings.

 

all I need it to do is get things in between <a href = "whatisinhere"> but it has to trim it to the domain name so www.doming.com hello.php.net or hwello.hello.php.net it can't have www.domain.com/what_ever_else_is_here.html If it doesnt have www. then its even better.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.