Start by looking at this : http://www.php.net/manual/function.parse-url.php
<?php
$url=parse_url("http://www.phpbuilder.com/directory/file.php3?name=Patrilia");
// here's what you need!
$host = $url["host"];
$first = strpos($host, ".");
$last = strpos($host, ".");
$length = strlen($host);
$result = substr($host, $first, $length-$last
);
echo "Result = |$result|<br>\n";
?>
That ought to be it, it might contain some errors or make some incorrect results, but it's the idea that counts. And please! Look into how the functions I've used actually work and you might learn something truly basic (that I have't learned yet!?)
Happy coding!