Jump to content

How to get the 'domain' part in www.domain.com/net/org?


ivytony

Recommended Posts

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;
?>

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.