I've been trying to work on this for a little while now using a combination of functions and ideas.

I am trying to get the domain from a url, which is easy enough if you want the sub-domain aswell, but i don't :/

Example

http://www.mydomain.com/dir/dir/index.html = mydomain.com
http://test.mydomain.com = mydomain.com
http://test.mydomain.co.uk = mydomain.co.uk

The main problem is that the domain extension could have 1 (.com,.net,.org etc..) or 2 (.co.uk, .ac.uk etc..) parts.

If anyone has managed to do something similar i would be really glad of the help.

Thanks very much, Craig

    You're looking for the function parse_url().

    $url ='http://www.whatever.com/path/to/some/directory/';
    extract(parse_url($url));
    

    Now you have a variable $host which has the value 'www.whatever.com', as well as a couple others you may be interested in, too: $scheme, $port, $user, $pass, $path, $query, and $fragment.

      Yeh but the problem is, that also includes the sub domain, which i need to get rid of.

        Figured a bit of imagination could get you through the rest ...

        The following works with every type of URL except the unlikely URL with more than one subdomain.

        extract(parse_url($url));
        $domain = explode('.', $host);
        array_shift($domain);
        
        if (count($domain) > 3) 
        	array_shift($domain);
        $host = implode('.', $domain);
        

          Thanks soo much, works perfectly 😃

            Write a Reply...