how to get the top level domain from an textstring?

Discussion in 'PHP' started by falcondriver, Nov 19, 2006.

  1. #1
    falcondriver, Nov 19, 2006 IP
  2. pixel_boost

    pixel_boost Peon

    Messages:
    55
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    there is a function in php that does this. i will search and prob in 10 min send you back ;)
     
    pixel_boost, Nov 19, 2006 IP
  3. pixel_boost

    pixel_boost Peon

    Messages:
    55
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    hmm... sorry, i think i was wrong earlier.
    but you can get that with regular expressions. now i'm really very busy but i think that later i can make u a little script that does what you want. good luck
     
    pixel_boost, Nov 19, 2006 IP
  4. streety

    streety Peon

    Messages:
    321
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #4
    streety, Nov 19, 2006 IP
  5. clancey

    clancey Peon

    Messages:
    1,099
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    0
    #5
    The variable you are looking for is captured as follows:

    $server = $_SERVER["SERVER_NAME"];

    This ignores trailing directories.

    Another way to accomplish the same result is:

    
    $domain = "http://www.test.com/test.html";
    
    list($x,$x,$domain,$x) = explode( "/", $domain, 4);
    
    echo $domain, "\n";
    
    PHP:
    But, this generates a PHP Notice when the URL has no trailing slash.
     
    clancey, Nov 19, 2006 IP
  6. falcondriver

    falcondriver Well-Known Member

    Messages:
    963
    Likes Received:
    47
    Best Answers:
    0
    Trophy Points:
    145
    #6
    thanks everyone, have it done now. thougt there was an easier way or a simple function for this, but i had to to it by myself:

    
    	function get_tld($url) {
    		$host = parse_url($url);
    		$domain = $host['host'];
    		$domain = str_replace("__", "", $domain);
    		$tail = substr($domain, -7);
    		$tld = strstr($tail, ".");
    		return $tld;
    	}
    
    Code (markup):
    i used pharse_url before, so this was a good start. for some reason i got results like "domain.com__" from pharse_url() sometimes, so i had to get rid of the underscores with str_replace. then i take the last 7 chars of this string (because the domain must be within the last 7) and return eveything after the first "." - success! :)
     
    falcondriver, Nov 19, 2006 IP