fanfavorite Posted January 23, 2009 Share Posted January 23, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/142181-check-if-domain-or-subdomain/ Share on other sites More sharing options...
printf Posted January 24, 2009 Share Posted January 24, 2009 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... Quote Link to comment https://forums.phpfreaks.com/topic/142181-check-if-domain-or-subdomain/#findComment-744976 Share on other sites More sharing options...
fanfavorite Posted January 24, 2009 Author Share Posted January 24, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/142181-check-if-domain-or-subdomain/#findComment-745022 Share on other sites More sharing options...
cwarn23 Posted January 24, 2009 Share Posted January 24, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/142181-check-if-domain-or-subdomain/#findComment-745146 Share on other sites More sharing options...
fanfavorite Posted January 24, 2009 Author Share Posted January 24, 2009 Thanks cwarn. It does the job! Quote Link to comment https://forums.phpfreaks.com/topic/142181-check-if-domain-or-subdomain/#findComment-745236 Share on other sites More sharing options...
Psycho Posted January 27, 2009 Share Posted January 27, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/142181-check-if-domain-or-subdomain/#findComment-747915 Share on other sites More sharing options...
nrg_alpha Posted January 27, 2009 Share Posted January 27, 2009 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.. Quote Link to comment https://forums.phpfreaks.com/topic/142181-check-if-domain-or-subdomain/#findComment-747943 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.