Get domain name from a URL

Its easy to get domain name or host from a URL using the builtin PHP function parse_url that can help you extract the domain name or other components of a given URL.

The below code extracts the domain name or host from a URL and then converts it to lower case and returns the string value.

function get_domain_from_url( $string )
{
    return strtolower( parse_url( $string , PHP_URL_HOST ) );
}

As a real world example, try this code:

get_domain_from_url( 'https://tools.pingdom.com/#!/dSbH2K/http://google.com/' );