I am parsing domains and running into a problem handling subdomains. If the domain is http://www.google.co.uk, I want to obtain the length of google
which is 6.
I am using parse_url()
to return the host in this case www.google.co.uk like so.
$url = 'http://www.google.co.uk';
$info = parse_url($url);
// remove www. and return google.co.uk
$new = str_replace('www.','',$info['host']);
$pieces = explode(".", $new);
$len = strlen($pieces[0]); // returns character length of google = 6
echo $len;
My code doesn't work if the domain contains a subdomain like http://test.google.co.uk: it returns a length of 4; I expect it to return a length of 6.
Any ideas?