SitePoint Sponsor

User Tag List

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

    Getting Domain Name Extension

    Hi All

    I need to get the domain name extension of a domain name.

    Example:

    whatever.com = .com

    whatever.com.au = .com.au

    whatever.net = .net

    I have no idea how to do this and have spent almost an hour trawling the forums trying to work it out.

    Any help would be great.

    Thanks

  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)
    a better means then using $parts[count($parts)-1];
    would be to use end()

    PHP Code:
    $parts split('.'$domain);
    $ext end($parts);

    # Or

    $ext end(split('.'$domain)); 
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,336
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    how do you know when you want the TLD only (e.g. .com) and when you want both the top and second level domain (e.g. .com.au, .co.uk)?

    how would you code for that?
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,863
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Presumably if the length of the TLD is 2 (country specific) you want the SLDL as well but if the length of the TLD is between 3 and 7 you only want that.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $url[0] = "http://www.mydomain.info/anydirectory/valid.php";
    $url[1] = "http://www.mydomain.org";
    $url[2] = "homepage.com/test.php";
    $url[3] = "homepage.gov";


    function 
    getURLExtension$url ) {
        
    $url ltrim$url"http://" );
        
    $parts explode"/"$url );
        
    $tmp explode"."$parts[0] );
        
        if ( 
    count $tmp ) >= ) {
            return 
    $tmp[2];
        } else {
            if ( 
    count $tmp ) == ) {
                    return 
    $tmp[1];
            }
        }
        return 
    FALSE;
    }
     
    foreach ( 
    $url as $K ) {
        echo 
    "The url extension is : " .getURLExtension$K ). "<br>\n";

    Yields :
    The url extension is : info
    The url extension is : org
    The url extension is : com
    The url extension is : gov
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  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)
    So something like this?
    PHP Code:
    <?php

    function getDomainTld ($domain) {

        
    $parts explode('.'$domain);
        
    $tld   array_pop($parts);

        if (
    strlen($tld) < 3) {
            
    $tld end($parts) . '.' $tld;
        }

        return 
    $tld;

    }


    $domain 'www.example.com';
    $domain 'www.example.co.uk';

    print 
    getDomainTld($domain); # co.uk
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  7. #7
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    With the previous posters code (byron3) combined it and mine togeather.

    Code php:
    <?php
     
    function getDomainHost ($url) {
     
        $url = strtolower($url);
     
        if (strstr($url, '://') !== false) {
     
            $url = parse_url($url);
            return $url['host']; # PHP5: return parse_url($url, PHP_URL_HOST);
    
        }
     
        $parts = explode('/', $url);
        return $parts[0];
     
    }
     
    function getDomainTld ($domain) {
     
        $domain = getDomainHost($domain);
     
        $parts = explode('.', $domain);
        $tld   = array_pop($parts);
     
        if (strlen($tld) < 3) {
            $tld = end($parts) . '.' . $tld;
        }
     
        return $tld;
     
    }
     
    $url[0] = 'http://www.mydomain.info/anydirectory/valid.php';
    $url[1] = 'http://www.mydomain.co.uk';
    $url[2] = 'homepage.com/test.php';
    $url[3] = 'homepage.gov';
    $url[4] = 'ftp://www.mydomain.com/valid.php';
     
     
    foreach ($url as $K) {
        echo getDomainTld($K) . "<br>\n";
    }

    It also correctly returns co.uk
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  8. #8
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,336
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by felgall View Post
    Presumably if the length of the TLD is 2 (country specific) you want the SLDL as well
    that would be wrong for most .ca sites, including mine
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  9. #9
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    One will probably need a list of all variations on TLDs but thats too bother guess having the end (.uk, .ca) is the best we can get.

    Or might mod the if statement in getDomainTld to:
    Code php:
    if (strlen($tld) < 3 && ($tmp = end($parts)) == 'co') {
        $tld = $tmp . '.' . $tld;
    }
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  10. #10
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    ...and .tv, .us, .ws, .to...

    You could always stick all the top level domains you want to match in an array and check if they're the end of the domain string.


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
  •