This is just that easy to do as picking up a dollar on the ground.
You can, of course write your own function to parse a string that looks like a URL and return various parts such as the domain, directory, requested file and possible data fields. However, PHP has come with exactly the function you need so you really don’t have to reinvent the wheel.
That is parse_url().
$url_parts = parse_url('https://www.google.com/search?q=obama');
The output looks like:
[scheme] => http
[host] => www.google.com
[path] => /search
[query] => q=obama
The returned is an array containing a breakdown of the given URL. This way not only do you have the host name (www.google.com) but also protocol scheme (http), server path (/search) and query string (q=obama).
Easy enough, huh?