Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 914053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:39:28+00:00 2026-05-15T17:39:28+00:00

I need to get the domain name from an URL. The following examples should

  • 0

I need to get the domain name from an URL. The following examples should all return google.com:

google.com
images.google.com
new.images.google.com
www.google.com

Similarly the following URLs should all return google.co.uk.

google.co.uk
images.google.co.uk
new.images.google.co.uk
http://www.google.co.uk

I’m hesitant to use Regular Expressions, because something like domain.com/google.com could return incorrect results.

How can I get the top-level domain, using PHP? This needs to work on all platforms and hosts.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T17:39:29+00:00Added an answer on May 15, 2026 at 5:39 pm

    You could do this:

    $urlData = parse_url($url);
    
    $host = $urlData['host'];
    

    ** Update **

    The best way I can think of is to have a mapping of all the TLDs that you want to handle, since certain TLDs can be tricky (co.uk).

    // you can add more to it if you want
    $urlMap = array('com', 'co.uk');
    
    $host = "";
    $url = "http://www.google.co.uk";
    
    $urlData = parse_url($url);
    $hostData = explode('.', $urlData['host']);
    $hostData = array_reverse($hostData);
    
    if(array_search($hostData[1] . '.' . $hostData[0], $urlMap) !== FALSE) {
      $host = $hostData[2] . '.' . $hostData[1] . '.' . $hostData[0];
    } elseif(array_search($hostData[0], $urlMap) !== FALSE) {
      $host = $hostData[1] . '.' . $hostData[0];
    }
    
    echo $host;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have got following folder structure for my domain (domain.com): /data/images (it´s subdomain name,
I am trying to get the domain name from the url with JSTL. The
I need to get the Windows domain name from Internet Explorer using classic asp
I am trying to get the profiles feed from my Google Apps domain using
I am using a URL to query some posts by their ID. http://domain.com/page-name/?id=123 Visitors
I am trying to get the NetBios name from a fully qualified Domain Name...
In my Django app, I need to get the host name from the referrer
For example there are URLs http://www.subdomain1.domain.com.uk and http://www.subdomain2.domain.uk , from these URLs I need
I need to remove the domain name from location.href using Javascript. I have links
I need an SSL certificate for an internal domain name, but all the providers

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.