Do you want the sub-domain portion, too, or just the domain? If the latter, it get's a little tricky with some of the 3-part domains, like "amzon.co.uk".
Anyway, I might approach it with preg_match, something like:
preg_match('#https?://([^/]+)(/|\s|$)#i', $string, $matches);
$domain = $matches[1];