Can PHP remove a TLD (.ca, etc) from a string and set a variable corresponding to it?

Discussion in 'PHP' started by interwho, Aug 17, 2010.

  1. #1
    I've searched for hours, and I can't seem to find a way to do this.

    I want to parse an input string (google.com), set a PHP variable corresponding to the TLD in the string, and remove the TLD from the string.

    ie:
    input 1 = google.com
    input 2 = google.net

    Lets say I'm inputting input 1 first, and input 2 next. I want this script to set a variable (.com = $com, .net = $net) to the TLD in the string, and remove the TLD from the string, setting the string to a new variable ($parseddomain).

    This would leave me with a string of google and a $com or $net variable.

    How can I do this?

    Sorry if this is confusing, I'm a PHP noob :)
     
    interwho, Aug 17, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    Heres a function I took from a project I did over a year ago:

    function get_tld($url) {
        $url = trim($url);
        
        if (!preg_match('~^http://~i', $url))
            $url = "http://{$url}";
        
        $domain = parse_url($url, PHP_URL_HOST);
        
        $domain = preg_replace('~^www\.~', NULL, strtolower($domain));
        
        $parts = explode('.', $domain);
        
        return (count($parts) > 2) ? ".{$parts[1]}.{$parts[2]}" : '.' . end($parts);
    }
    PHP:
    Integrate it in your scenario...

    <?php
    function get_tld($url) {
        $url = trim($url);
        
        if (!preg_match('~^http://~i', $url))
            $url = "http://{$url}";
        
        $domain = parse_url($url, PHP_URL_HOST);
        
        $domain = preg_replace('~^www\.~', NULL, strtolower($domain));
        
        $parts = explode('.', $domain);
        
        return (count($parts) > 2) ? ".{$parts[1]}.{$parts[2]}" : '.' . end($parts);
    }
    
    $domain = 'google.com';
    
    //.com
    $tld = get_tld($domain);
    
    //google
    $new_domain = str_replace($tld, NULL, $domain);
    
    ?>
    PHP:
     
    danx10, Aug 17, 2010 IP
  3. interwho

    interwho Member

    Messages:
    198
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    30
    #3
    interwho, Aug 17, 2010 IP
  4. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #4
    Would work in both cases.
     
    danx10, Aug 17, 2010 IP
  5. interwho

    interwho Member

    Messages:
    198
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    30
    #5
    For some reason I keep getting this error:

    Warning: parse_url(http://) [function.parse-url]: Unable to parse URL in /home2/account/public_html/ams/scriptname.php on line 27...

    Any suggestions? I modified the code to this:

    
    function parse_tld($url) {
        $url = trim($url);
        
        if (!preg_match('~^http://~i', $url))
            $url = "http://{$url}";
        
        $inputdomain = parse_url($url, PHP_URL_HOST);
        
        $inputdomain = preg_replace('~^www\.~', NULL, strtolower($inputdomain));
        
        $parts = explode('.', $inputdomain);
        
        return (count($parts) > 2) ? ".{$parts[1]}.{$parts[2]}" : '.' . end($parts);
    }
    $tld = parse_tld($inputdomain);
    $parseddomain = str_replace($tld, NULL, $inputdomain);
    
    PHP:
    The $inputdomain was defined in the script, its just not shown here.
     
    interwho, Aug 17, 2010 IP
  6. Narrator

    Narrator Active Member

    Messages:
    392
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    80
    #6
    I am pretty sure it would only throw that error if $inputdomain is null or defined as 'http://' because there is nothing to parse.
     
    Narrator, Aug 17, 2010 IP
  7. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #7
    echo $inputdomain and reply quoting it.
     
    danx10, Aug 17, 2010 IP
  8. interwho

    interwho Member

    Messages:
    198
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    30
    #8
    nevermind, fixed it, thanks! +1 Rep for all who helped :)
     
    interwho, Aug 17, 2010 IP