SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Evangelist
    Join Date
    Nov 2003
    Location
    Brisbane, Australia
    Posts
    408
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Get Just the Domain Name

    Hi All

    I need to get just the domian name and none of the extra info about it. e.g. www.somedomain.com. All I need to get is somedomain.

    I have $_SERVER['HTTP_HOST'] but how do I split it up?

    Thanks for any help.

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Evangelist
    Join Date
    Nov 2003
    Location
    Brisbane, Australia
    Posts
    408
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for that but that is a huge page and I have looked through it but as I am not too sure exactly what I am looking for I have had no luck. A little more help would be great.

    Thanks

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    http://us3.php.net/manual/en/function.explode.php

    Being able to read the PHP manual is just as important as the programming skills.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    SitePoint Addict nimasdj's Avatar
    Join Date
    Dec 2007
    Posts
    314
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    better to use regular expression to remove www. rather than explode

  6. #6
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by nimasdj View Post
    better to use regular expression to remove www. rather than explode
    Why? Why is it better to use far slower regex?
    There is also the TLD, not just "www."
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  7. #7
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    There are a myriad ways to parse the string www.somedomain.com and retrieve only somedomain and your preferred method might well be different depending on what exactly is being parsed, if/how it can vary, etc..

    The following all give us somedomain yet may be entirely not what you want to use:
    • $some = preg_replace('/^(?:www\.)?([a-z-]+)(?:\.com)$/', '$1', 'www.somedomain.com');
    • $domains = explode('.', 'www.somedomain.com', 3);
      $some = $domains[1];
    • $domain = 'www.somedomain.com';
      $offset = strpos($domain, '.') + 1;
      $length = strcspn($domain, '.', $offset);
      $some = substr($domain, $offset, $length);


    Off Topic:

    This may or may not be relevant to your example, but the $_SERVER['HTTP_HOST'] value is just what is sent by the client within the Host header. As such, it can be modified by the client and may not be the value that you expect!

    $_SERVER['SERVER_NAME'] may contain a more useful value, assuming your server is set up to report what you want (in this case, your domain name).
    Salathe
    Software Developer and PHP Manual Author.

  8. #8
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For something this simple regex is too much overhead. Use the string functions described.
    Fiji Web Design - Enterprise Web Design

  9. #9
    SitePoint Zealot
    Join Date
    May 2009
    Location
    usa
    Posts
    113
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why don't you use the simple str_pos() and substr() functions?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •