Currently, I can strip the domain of the URL string by:
$pattern = '/\w+\..{2,3}(?:\..{2,3})?(?:$|(?=\/))/i';
$url = 'http://www.example.com/foo/bar?hat=bowler&accessory=cane';
if (preg_match($pattern, $url, $matches) === 1) {
echo $matches[0];
}
This will echo:
example.com
The problem is that I want to include the subdomain if it exists.
So for example:
$url = 'http://sub.domain.example.com/foo/bar?hat=bowler&accessory=cane';
Should echo:
sub.domain.example.com
How can I achieve this insanity?