1,105,208 Community Members

Filter url to domain

Member Avatar
Reputation Points: 387 [?]
Q&As Helped to Solve: 281 [?]
Skill Endorsements: 15 [?]
 
0
 

In these three functions, you can filter to a url to just the domain name. There are three main methods of doing so which are all shown in the example and the usage of each function is shown in the example.

<?php
//function one - get entire domain
function fulldomain($domainb) {
    $bits = explode('/', $domainb);
    if ($bits[0]=='http:' || $bits[0]=='https:')
        {
        return $bits[0].'//'.$bits[2].'/';
        } else {
        return 'http://'.$bits[0].'/';
        }
    unset($bits);
    }

//function two - use regex to get entire domain
function preg_fulldomain ($domainb) {
    return 

preg_replace('/^((http(s)?:\/\/)?([^\/]+)(\/)?)(.*)/','$1',$domain

b);
    }

//function three - get domain and remove subdomain.
function domain($domainb)
	{
	$bits = explode('/', $domainb);
	if ($bits[0]=='http:' || $bits[0]=='https:')
		{
		$domainb= $bits[2];
		} else {
		$domainb= $bits[0];
		}
	unset($bits);
	$bits = explode('.', $domainb);
	$idz=count($bits);
	$idz-=3;
	if (strlen($bits[($idz+2)])==2) {
	$url=$bits[$idz].'.'.$bits[($idz+1)].'.'.$bits[($idz+2)];
	} else if (strlen($bits[($idz+2)])==0) {
	$url=$bits[($idz)].'.'.$bits[($idz+1)];
	} else {
	$url=$bits[($idz+1)].'.'.$bits[($idz+2)];
	}
	return $url;
	}


//below is the usage of each function
$address='http://www.subdomain.example.com/blog/index.php?id=1';
echo fulldomain($address);
echo '<br>';
echo preg_fulldomain($address);
echo '<br>';
echo domain($address);
?>
Member Avatar
ShawnCplus
Code Monkey
1,564 posts since Apr 2005
Reputation Points: 456 [?]
Q&As Helped to Solve: 269 [?]
Skill Endorsements: 39 [?]
Team Colleague
 
2
 
Member Avatar
NicAx64
Posting Pro
543 posts since Mar 2009
Reputation Points: 76 [?]
Q&As Helped to Solve: 46 [?]
Skill Endorsements: 0 [?]
 
0
 

You have used the preg instead of ereg I think it's a good Patrice .

Member Avatar
whatsgoingon
Newbie Poster
1 post since Oct 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
1
 

Agree that using parse_url could help. Thanks for writing this

Member Avatar
roninio
Newbie Poster
2 posts since Feb 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

$parseUrl = parse_url(trim($Address));
$bits=explode('.', $parseUrl['host' );
unset($bits[0]);
$domain = implode('.',$bits);

Member Avatar
kthxbai2u
Newbie Poster
1 post since Dec 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

ronino: Thanks, I used your code... And modified it too! The following code will only remove the subdomain if it exists... If you run http://domain.com through ronino's code, it would come out as "com". This is a simple fix to check there are more than 2 "bits".

$parseUrl = parse_url(trim($filename));
	$bits=explode('.', $parseUrl['host']);
	if (count($bits) > 2) {
		unset($bits[0]);
	}
	$domain = implode('.',$bits);

Enjoy ;P
http://kthxbai2u.com/

***Note: The above code only works on single . TLD's (.com, .net, .org) If you get "fasthost.co.uk", it will come out as "co.uk"***
I am working on a fix :)

[EDIT]

I ended up using another code from elsewhere which works flawlessly so far. Here it is:

function getDomain($domainb)
	{
	$bits = explode('/', $domainb);
	if ($bits[0]=='http:' || $bits[0]=='https:')
		{
		$domainb= $bits[2];
		} else {
		$domainb= $bits[0];
		}
	unset($bits);
	$bits = explode('.', $domainb);
	$idz=count($bits);
	$idz-=3;
	if (strlen($bits[($idz+2)])==2) {
	$url=$bits[$idz].'.'.$bits[($idz+1)].'.'.$bits[($idz+2)];
	} else if (strlen($bits[($idz+2)])==0) {
	$url=$bits[($idz)].'.'.$bits[($idz+1)];
	} else {
	$url=$bits[($idz+1)].'.'.$bits[($idz+2)];
	}
	return $url;
	}
Member Avatar
huongviet
Newbie Poster
3 posts since Dec 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

Thank you for code. I will try this code in my application, I hope it will good work

Member Avatar
gtuminauskas
Newbie Poster
1 post since Jun 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

if I write full url like this: 'ftp://www.subdomain.example.com/download/file.zip'
then it gives me completely wrong values..
not too wise enough! domain becomes ".ftp" and not "example.com"

Member Avatar
diafol
Where are my eyes?
12,941 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,845 [?]
Skill Endorsements: 90 [?]
Moderator
Featured
Sponsor
 
0
 

This is why we user parse_url()

You
Post:
Start New Discussion
View similar articles that have also been tagged: