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 264731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:42:36+00:00 2026-05-11T22:42:36+00:00

I have an url like this: http://www.w3schools.com/PHP/func_string_str_split.asp I want to split that url to

  • 0

I have an url like this:

http://www.w3schools.com/PHP/func_string_str_split.asp

I want to split that url to get the host part only. For that I am using

parse_url($url,PHP_URL_HOST);

it returns http://www.w3schools.com.
I want to get only ‘w3schools.com’.
is there any function for that or do i have to do it manually?

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

    There are many ways you could do this. A simple replace is the fastest if you know you always want to strip off ‘www.’

    $stripped=str_replace('www.', '', $domain);
    

    A regex replace lets you bind that match to the start of the string:

    $stripped=preg_replace('/^www\./', '', $domain);
    

    If it’s always the first part of the domain, regardless of whether its www, you could use explode/implode. Though it’s easy to read, it’s the most inefficient method:

    $parts=explode('.', $domain);
    array_shift($parts); //eat first element
    $stripped=implode('.', $parts);
    

    A regex achieves the same goal more efficiently:

    $stripped=preg_replace('/^\w+\./', '', $domain);
    

    Now you might imagine that the following would be more efficient than the above regex:

    $period=strpos($domain, '.');
    if ($period!==false)
    {
        $stripped=substr($domain,$period+1);
    }
    else
    {
        $stripped=$domain; //there was no period
    }
    

    But I benchmarked it and found that over a million iterations, the preg_replace version consistently beat it. Typical results, normalized to the fastest (so it has a unitless time of 1):

    • Simple str_replace: 1
    • preg_replace with /^\w+\./: 1.494
    • strpos/substr: 1.982
    • explode/implode: 2.472

    The above code samples always strip the first domain component, so will work just fine on domains like “www.example.com” and “www.example.co.uk” but not “example.com” or “www.department.example.com”. If you need to handle domains that may already be the main domain, or have multiple subdomains (such as “foo.bar.baz.example.com”) and want to reduce them to just the main domain (“example.com”), try the following. The first sample in each approach returns only the last two domain components, so won’t work with “co.uk”-like domains.

    • explode:

      $parts = explode('.', $domain);
      $parts = array_slice($parts, -2);
      $stripped = implode('.', $parts);
      

      Since explode is consistently the slowest approach, there’s little point in writing a version that handles “co.uk”.

    • regex:

      $stripped=preg_replace('/^.*?([^.]+\.[^.]*)$/', '$1', $domain);
      

      This captures the final two parts from the domain and replaces the full string value with the captured part. With multiple subdomains, all the leading parts get stripped.

      To work with “.co.uk”-like domains as well as a variable number of subdomains, try:

      $stripped=preg_replace('/^.*?([^.]+\.(?:[^.]*|[^.]{2}\.[^.]{2}))$/', '$1', $domain);
      
    • str:

      $end = strrpos($domain, '.') - strlen($domain) - 1;
      $period = strrpos($domain, '.', $end);
      if ($period !== false) {
          $stripped = substr($domain,$period+1);
      } else {
          $stripped = $domain;
      }
      

      Allowing for co.uk domains:

      $len = strlen($domain);
      if ($len < 7) {
          $stripped = $domain;
      } else {
          if ($domain[$len-3] === '.' && $domain[$len-6] === '.') {
              $offset = -7;
          } else {
              $offset = -5;
          }
          $period = strrpos($domain, '.', $offset);
          if ($period !== FALSE) {
              $stripped = substr($domain,$period+1);
          } else {
              $stripped = $domain;
          }
      }
      

    The regex and str-based implementations can be made ever-so-slightly faster by sacrificing edge cases (where the primary domain component is a single letter, e.g. “a.com”):

    • regex:

      $stripped=preg_replace('/^.*?([^.]{3,}\.(?:[^.]+|[^.]{2}\.[^.]{2}))$/', '$1', $domain);
      
    • str:

      $period = strrpos($domain, '.', -7);
      if ($period !== FALSE) {
          $stripped = substr($domain,$period+1);
      } else {
          $stripped = $domain;
      }
      

    Though the behavior is changed, the rankings aren’t (most of the time). Here they are, with times normalized to the quickest.

    • multiple subdomain regex: 1
    • .co.uk regex (fast): 1.01
    • .co.uk str (fast): 1.056
    • .co.uk regex (correct): 1.1
    • .co.uk str (correct): 1.127
    • multiple subdomain str: 1.282
    • multiple subdomain explode: 1.305

    Here, the difference between times is so small that it wasn’t unusual for . The fast .co.uk regex, for example, often beat the basic multiple subdomain regex. Thus, the exact implementation shouldn’t have a noticeable impact on speed. Instead, pick one based on simplicity and clarity. As long as you don’t need to handle .co.uk domains, that would be the multiple subdomain regex approach.

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

Sidebar

Ask A Question

Stats

  • Questions 218k
  • Answers 219k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I've solved it. I think. I used Element.match() $('create_course').addEvent('submit', function(e){… May 12, 2026 at 11:40 pm
  • Editorial Team
    Editorial Team added an answer SELECT max_table.namecode, count_table2.name FROM (SELECT namecode, MAX(count_name) AS max_count FROM… May 12, 2026 at 11:40 pm
  • Editorial Team
    Editorial Team added an answer How to troubleshoot this error right now: In VIM, pick… May 12, 2026 at 11:40 pm

Related Questions

If I have a URL like this: http://www.example.com/?a=1&b=2&c=3 (an example) I am working on
I am developing an iPhone application in which I am fetching data from a
I worked on a CMS and I want to have diffrent Buttons for special
I have set up an ASP.NET MVC project, and everything is working great, but

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.