Reputation: 1212
To get the domain name i am using this code:
<?php
$myURL = 'http://answers.yahoo.com/question/index?qid=20130406061745AAmovgl';
$pattern = '/\w+\..{2,3}(?:\..{2,3})?(?:$|(?=\/))/i';
if (preg_match($pattern, $myURL, $domain) === 1) {
$domain = $domain[0];
}
$ndomain = "http://$domain";
echo $ndomain;
?>
but it will output: http://yahoo.com
But, how i can output http://answers.yahoo.com
this sub-domain exactly.
Upvotes: 2
Views: 826
Reputation: 2412
You can use parse_url()
like this:
$urlData = parse_url($myURL);
$host = $urlData['host']; //domain + subdomain
Upvotes: 3