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

  • Home
  • SEARCH
  • 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 94597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:31:36+00:00 2026-05-10T23:31:36+00:00

PHP’s parse_url() has a host field, which includes the full host. I’m looking for

  • 0

PHP’s parse_url() has a host field, which includes the full host. I’m looking for the most reliable (and least costly) way to only return the domain and TLD.

Given the examples:

  • http://www.google.com/foo, parse_url() returns http://www.google.com for host
  • http://www.google.co.uk/foo, parse_url() returns http://www.google.co.uk for host

I am looking for only google.com or google.co.uk. I have contemplated a table of valid TLD’s/suffixes and only allowing those and one word. Would you do it any other way? Does anyone know of a pre-canned valid REGEX for this sort of thing?

  • 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. 2026-05-10T23:31:36+00:00Added an answer on May 10, 2026 at 11:31 pm

    How about something like that?

    function getDomain($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; } 

    Will extract the domain name using the classic parse_url and then look for a valid domain without any subdomain (www being a subdomain). Won’t work on things like ‘localhost’. Will return false if it didn’t match anything.

    // Edit:

    Try it out with:

    echo getDomain('http://www.google.com/test.html') . '<br/>'; echo getDomain('https://news.google.co.uk/?id=12345') . '<br/>'; echo getDomain('http://my.subdomain.google.com/directory1/page.php?id=abc') . '<br/>'; echo getDomain('https://testing.multiple.subdomain.google.co.uk/') . '<br/>'; echo getDomain('http://nothingelsethan.com') . '<br/>'; 

    And it should return:

    google.com google.co.uk google.com google.co.uk nothingelsethan.com 

    Of course, it won’t return anything if it doesn’t get through parse_url, so make sure it’s a well-formed URL.

    // Addendum:

    Alnitak is right. The solution presented above will work in most cases but not necessarily all and needs to be maintained to make sure, for example, that their aren’t new TLD with .morethan6characters and so on. The only reliable way of extracting the domain is to use a maintained list such as http://publicsuffix.org/. It’s more painful at first but easier and more robust on the long-term. You need to make sure you understand the pros and cons of each method and how it fits with your project.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 67k
  • Answers 67k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer It seems as though it was a network configuration problem.… May 11, 2026 at 11:50 am
  • added an answer Definitely, the easiest way for you will be using C#… May 11, 2026 at 11:50 am
  • added an answer I think you need to clarify the issue here. I… May 11, 2026 at 11:50 am

Related Questions

PHP has a great function called htmlspecialcharacters() where you pass it a string and
PHP's explode function returns an array of strings split on some provided substring. It
PHP, as we all know is very loosely typed. The language does not require
PHP stores its session information on the file system of the host of the
PHP treats all arrays as associative, so there aren't any built in functions. Can
PHP has a number of opcode caches, which as i understand it are scripts
PHP 4.4 and PHP 5.2.3 under Apache 2.2.4 on ubuntu. I am running Moodle
PHP has a very nice function, isset($variableName). It checks if $variableName is already defined
PHP (among others) will execute the deepest function first, working its way out. For
PHP, for all its warts, is pretty good on this count. There's no difference

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.