Getting the domain name out of a URL

 

<?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”;
?>

 

 

domain name is: php.net

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

AddThis Social Bookmark Button

Leave a Reply

Spam protection by WP Captcha-Free