In web programming, sometimes we need to get the domain name out of the url. Here how to do it:
<?php
// get host name from URL
preg_match(‘@^(?:http://)?([^/]+)@i’, “http://www.php.net/index.html“, $matches);
$host = $matches[1];
// get last two segments of host name
preg_match(‘/[^.]+\.[^.]+$/’, $host, $matches);
echo “domain name is: {$matches[0]}\n”;
?>
What’s the hell that code means!