For one of my projects I had to find the exact domain name extension using a php script. While, this doesn’t seems too hard to do for a simple domain name (i.e domain.com) by just exploding the domain string and separating the two arrays items by the dot character, I had to do it a little bit more .. sophisticated let say. Generally a domain name string should look like domain.com, but what if the user types www.domain.com or even worse my.section.at.department.university-name.edu.
Obviously, exploding the string wouldn’t be the best way to determine the extension, as I don’t know the exact number of items the array will have (I can count the dot characters in the string and then just fetch the array item corresponding to the counted value, but that seems too much CPU cycles to me). So I came up with the following solution:
$extension = substr($domain, strrpos($domain, “.”)+1);
What the above line does is to return all the characters from the domain that are found after the last dot character. You will notice the +1 value. I’ve pushed one additional character further in the string simply because I needed the exact extension without the dot. If you need to display let say ‘.com’ or ‘.org’ rather than ‘com’ or ‘org’ you can simply remove the +1 addition from the sttrpos section.
This isn’t really something big ;-) I wrote it here so that I can remember how I did in the future.. and of course to help if someone else needs that functionality.
EDIT: ALTERNATIVE WAY TO DO IT
Ok.. after some testing, it seems that the `sophisticated` method above is not really that sophisticated. If an attacker or an extremely stupid visitor types ‘mydomaincom’ without a single dot in the domain string, the above function will just return the whole domain string instead of false or .. anything that might help us determine it wasn’t the expected outcome.
So .. we are back to the beginning. It seems we’ll use explode after all. What I’m currently using to solve this is:
$ext_array = explode(‘.’,$domain);
if(sizeof($ext_array) > 1){
$extension = $ext_array[sizeof($ext_array)-1];
} else {
// Display error or do something else here …
}
This is it :) We now explode the string and return the very last element of it only and only if the exploded array has more than 1 element (that is if the domain has a dot in it). Yet, this requires more CPU cycles than the above and I recommend you to use the previous one if you are absolutely sure that the domain name contains a dot character and you have already ran some verification checks on it.
Yes, I too would have used explode, too.
Coz looking at your initial solution, it would not work in every situation coz if there was no dot (.) in the $domain variable, then strrpos would return false, then you would effectively be making this call:
$extension = substr($domain, FALSE+1); //false in lowercase if you like.
Perhaps that could translate to substr($domain,1), which would output everything from the 2nd character of $domain going forward.
So I’m glad you ended up using explode() instead. That’s why it’s part of PHP.
…A mouthful by Tashman :)
Some extensions have more than one dot!
.co.uk
.com.au
etc
Here’s my version: (gets the current domain extension)
$temparray=explode(‘.’,trim($_SERVER['HTTP_HOST'],”www.”));
$domain=array_shift($temparray);
$domainExt=join(‘.’,$temparray);
Ciarán