2

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.

2 Answers 2

8

You should instead use the parse_url function, since it exists to do this very thing.

echo parse_url( $url, PHP_URL_HOST );
Sign up to request clarification or add additional context in comments.

Comments

3

You can use parse_url()like this:

$urlData = parse_url($myURL);
$host = $urlData['host']; //domain + subdomain

Comments

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.