1

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.

4
  • Sorry i have found solution, please dont ban Commented May 21, 2014 at 6:18
  • 2
    Then you're free to answer your question and even mark the answer as accepted. It's absolutely legit on SO. Commented May 21, 2014 at 6:19
  • Its fun, but sollution which i have found is not correct, becase its dont cut www. then the problem is still actual. Commented May 21, 2014 at 9:30
  • I don't get exactly what you want, for me parse_url provides the domain name. Please, give some sample input strings and desired result. Commented May 21, 2014 at 13:00

1 Answer 1

1

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'.

Sign up to request clarification or add additional context in comments.

1 Comment

what will be if domain name is somedomain.co.im

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.