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. Link to comment https://forums.phpfreaks.com/topic/161273-solved-stripping-subdomain-from-url/ 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 Link to comment https://forums.phpfreaks.com/topic/161273-solved-stripping-subdomain-from-url/#findComment-850994 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. Link to comment https://forums.phpfreaks.com/topic/161273-solved-stripping-subdomain-from-url/#findComment-850995 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; ?> Link to comment https://forums.phpfreaks.com/topic/161273-solved-stripping-subdomain-from-url/#findComment-850999 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/. Link to comment https://forums.phpfreaks.com/topic/161273-solved-stripping-subdomain-from-url/#findComment-851004 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.