ivytony Posted January 22, 2009 Share Posted January 22, 2009 I am able to get the 'domain' part from a domain like www.domain.com using below code. I wonder if there's any more efficient way to do this. Thanks <?php function str_between($string, $start, $end){ $string = " ".$string; $ini = strpos($string,$start); if ($ini == 0) return ""; $ini += strlen($start); $len = strpos($string,$end,$ini) - $ini; return substr($string,$ini,$len); } $url = 'http://www.youtube.com/path?arg=value#anchor'; $domain = str_between($url, ".", "."); echo $domain; ?> Link to comment https://forums.phpfreaks.com/topic/141987-how-to-get-the-domain-part-in-wwwdomaincomnetorg/ Share on other sites More sharing options...
gevans Posted January 22, 2009 Share Posted January 22, 2009 echo $_SERVER["HTTP_HOST"] that should be what you need Link to comment https://forums.phpfreaks.com/topic/141987-how-to-get-the-domain-part-in-wwwdomaincomnetorg/#findComment-743438 Share on other sites More sharing options...
aebstract Posted January 22, 2009 Share Posted January 22, 2009 You should be able to use explode, something like: explode('.', $urlhere); Then the second value in the array should be your domain. Would split up like: 0 - http://www 1 - youtube 2 - com/path?etcetc Link to comment https://forums.phpfreaks.com/topic/141987-how-to-get-the-domain-part-in-wwwdomaincomnetorg/#findComment-743439 Share on other sites More sharing options...
ivytony Posted January 22, 2009 Author Share Posted January 22, 2009 thanks guys! the explode function works very well but what if the $url is 'youtube.com/path?arg=value#anchor' without the www part? Link to comment https://forums.phpfreaks.com/topic/141987-how-to-get-the-domain-part-in-wwwdomaincomnetorg/#findComment-743448 Share on other sites More sharing options...
gevans Posted January 22, 2009 Share Posted January 22, 2009 thanks guys! the explode function works very well but what if the $url is 'youtube.com/path?arg=value#anchor' without the www part? in which case you could use..... tada... echo $_SERVER["HTTP_HOST"] that should be what you need Link to comment https://forums.phpfreaks.com/topic/141987-how-to-get-the-domain-part-in-wwwdomaincomnetorg/#findComment-743566 Share on other sites More sharing options...
Zane Posted January 22, 2009 Share Posted January 22, 2009 there is also parse_url Link to comment https://forums.phpfreaks.com/topic/141987-how-to-get-the-domain-part-in-wwwdomaincomnetorg/#findComment-743571 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.