Reputation: 41
Im using the following function to cut domain from string:
function get_domain($url)
{
$pieces = parse_url($url);
$domain = isset($pieces['host']) ? $pieces['host'] : '';
if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) {
return $regs['domain'];
}
return false;
}
I need to cut subdomain + domain how should i change preg_match
to get it?
PS i was searching solution but everyone wants to cut only domain without sub.
Upvotes: 1
Views: 601
Reputation: 7447
If you can't work out the regexp, a more procedural approach might be:
$pieces = parse_url($url);
$aDomains = explode('.', $pieces['host']);
$sub = array_shift($aDomains);
$restofdomain = implode($aDomains);
...if you're always going to just want the first domain (i.e. it wouldn't work with a root domain like 'somedomain.com'.
Upvotes: 1