wwfc_barmy_army Posted June 7, 2009 Share Posted June 7, 2009 Hello. I have a form for where a domain is submitted. Although i only need the base domain so no subdomains. I'm guessing it will be some kind of str_replace or preg_match but i'm unsure what it would be. So if for example mail.google.com was submitted the value i want is google.com, and if google.com is submitted i want google.com. Thanks for any advice/code. Quote Link to comment Share on other sites More sharing options...
phpdragon Posted June 7, 2009 Share Posted June 7, 2009 <?php $test="mail.google.com"; $break=explode('.', $test); $result=$break[1]."."$break[2]; echo $result; ?> edit: this only works for first level domains tho I have to look up the other way of doing it thats a bit more reliable Quote Link to comment Share on other sites More sharing options...
thebadbad Posted June 7, 2009 Share Posted June 7, 2009 Have a look at parse_url(). Edit: Actually, it doesn't seem like the host-only is returned by that function. Using explode(): <?php $str = 'mail.google.com'; $parts = array_reverse(explode('.', $str)); $host = "{$parts[1]}.{$parts[0]}"; ?> By reversing the array we get around the problem phpdragon ran into. Quote Link to comment Share on other sites More sharing options...
phpdragon Posted June 7, 2009 Share Posted June 7, 2009 This seems to work ok <?php $test="mail.google.com.au"; $break=explode('.', $test); if (isset($break[3])) { $result="$break[1].$break[2].$break[3]"; } else { $result="$break[1].$break[2]"; } echo $result; ?> Quote Link to comment Share on other sites More sharing options...
thebadbad Posted June 7, 2009 Share Posted June 7, 2009 Oh, forgot the 'double' TLDs. I was only accounting for sub-sub domains like http://forum.developers.facebook.com/. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.