CodeIgniter Forums
Acquire the url prefix? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Acquire the url prefix? (/showthread.php?tid=51402)



Acquire the url prefix? - El Forum - 05-02-2012

[eluser]coldscooter[/eluser]
Is there any nice way of detecting the url prefix?

so for this url:

http://test.mysite.com

I would like to extract 'test'



Acquire the url prefix? - El Forum - 05-02-2012

[eluser]CroNiX[/eluser]
They're "subdomains", not "prefixes". This will probably work, but it will only return the first subdomin. For instance, if your url was http://some.subdomain.domain.com, it would only return "some". If there isn't a subdomain, like http://domain.com, it will return boolean FALSE.
Code:
function get_subdomain($url)
{
  $parts = parse_url($url);
  $parts = explode('.', $parts['host']);
  return (count($parts) > 2) ? $parts[0] : FALSE;
}

$subdomain = get_subdomain('http://test.domain.com');
var_dump($subdomain);
// "test"

$subdomain = get_subdomain('http://domain.com');
var_dump($subdomain);

// (bool)FALSE



Acquire the url prefix? - El Forum - 05-02-2012

[eluser]CroNiX[/eluser]
Strange, the forum software is inserting commas where there are none.