Extract Domain Name From Hostname in PHP

When fighting spammers or other unwanted traffic it’s useful to be able to block a top level domain, such as ukhost4u.com or uk2.net. This needs to be extracted from the full hostname, which might be something like 123-456-789-10.ukhost4u.com. To do that you’ll need a combination of a PHP built-in function and a regular expression.