I came up with this, however it fails with multiple TLDs like in your 4th example in the OP:
$url = 'http://www.example.com/page.php?q=something#anchor';
preg_match('@^(https?://)?([a-z0-9_-]+\.)*([a-z0-9_-]+)\.[a-z]{2,6}(/.*)?$@i',$url,$match);
$domain = $match[3];