This isnt perfect (you may want to refer to the relevant RFCs), but you can try something along the lines of:
$pattern = '/([a-z0-9\-]{1,63}.[a-z]{3}(.[a-z]{2})?)$/i';
$domain = 'example.com';
if (preg_match($pattern, $domain, $matches)) {
echo $matches[1];
}