Jump to content

Recommended Posts

Hey All,

 

I was wondering if anyone knew a way to check if a submitted url is a domain or subdomain.  For example possible entries are:

 

cname.somedomain.com

somedomain.com

 

cname.somedomain.on.ca

somedomain.on.ca

 

For regular domains, we use: 

 

function isValidSLD($sld) {
if(preg_match('/^[a-z0-9]+[a-z0-9\-]*[a-z0-9]+$/i', $sld) && strlen($sld) < 64 && substr($sld, 2, 2) != '--') {
	return true;
} else {
	return false;
}
}

function checkvalidDomain($domainName) {
if (!preg_match('/^([a-z0-9]+[a-z0-9\-]*[a-z0-9]+)\.([a-z]+[a-z\.]*[a-z]+)$/i', $domainName, $parts) || !$this->isValidSLD($parts[1])) {
	return false;
}
return true;
}

 

However this will only return true if its a regular domain.  Any help is appreciated. 

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/142181-check-if-domain-or-subdomain/
Share on other sites

Neither of those truly validate a domain, anyone can write domain.apple, and that wouldn't be valid because there is no TLD called apple! Just checking the format and validating is two different things. Also there are valid (3) level TLD too! Not just (2) Level TLDs...

I am aware that it is not foolproof, but it does it's purpose.  I would like to at least detect if its a subdomain or regular domain, even if the tld is invalid.  I would love to get it complete it to check for valid domains too, but not completely necessary at this point.  Do you have any solutions for what I am trying to do? 

Hey All,

 

I was wondering if anyone knew a way to check if a submitted url is a domain or subdomain.  For example possible entries are:

 

cname.somedomain.com

somedomain.com

 

cname.somedomain.on.ca

somedomain.on.ca

I have just read the question and I had already made a function that strips a url to the domain+subdomain so I have extended it to determine the real domain. My code is as follows:

<?
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=0;
while (isset($bits[$idz]))
	{
	$idz+=1;
	}
$idz-=3;
$idy=0;
while ($idy<$idz)
	{
	unset($bits[$idy]);
	$idy+=1;
	}
$part=array();
foreach ($bits AS $bit)
	{
	$part[]=$bit;
	}
unset($bit);
unset($bits);
unset($domainb);
if (strlen($part[1])>3)
	{
	unset($part[0]);
	}
foreach($part AS $bit)
	{
	$domainb.=$bit.'.';
	}
unset($bit);
return preg_replace('/(.*)\./','$1',$domainb);
}


//Now to use the function
echo domain('http://www.subdomain.google.com.au/');
?>

And that is all it takes. Not much regex. Mainly the explode function does the job.

Enjoy.

Neither of those truly validate a domain, anyone can write domain.apple, and that wouldn't be valid because there is no TLD called apple!

 

There soon will be! As well as any other TLD that anyone wishes to create. In June of last year ICANN approved a proposal to allow custom Top Level Domains.

Neither of those truly validate a domain, anyone can write domain.apple, and that wouldn't be valid because there is no TLD called apple!

 

There soon will be! As well as any other TLD that anyone wishes to create. In June of last year ICANN approved a proposal to allow custom Top Level Domains.

 

Here is the link with regards to what mjdamato is talking about..

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.