Last active
August 29, 2015 14:15
-
-
Save chrisblakley/bc093bf91caed959aef4 to your computer and use it in GitHub Desktop.
Easily pull the segment of a URL without constantly re-writing code. Documentation here: http://gearside.com/php-function-easily-return-url-components/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Get the full URL. Not intended for secure use ($_SERVER var can be manipulated by client/server). | |
function gearside_requested_url($host="HTTP_HOST") { //Can use "SERVER_NAME" as an alternative to "HTTP_HOST". | |
$protocol = ( (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443 ) ? 'https' : 'http'; | |
$full_url = $protocol . '://' . $_SERVER["$host"] . $_SERVER["REQUEST_URI"]; | |
return $full_url; | |
} | |
//Separate a URL into it's components. | |
//See http://gearside.com/php-function-easily-return-url-components/ for documentation | |
function gearside_url_components($segment="all", $url=null) { | |
if ( !$url ) { | |
$url = nebula_requested_url(); | |
} | |
$url_compontents = parse_url($url); | |
if ( empty($url_compontents['host']) ) { | |
return; | |
} | |
$host = explode('.', $url_compontents['host']); | |
//Best way to get the domain so far. Probably a better way by checking against all known TLDs. | |
preg_match("/[a-z0-9\-]{1,63}\.[a-z\.]{2,6}$/", parse_url($url, PHP_URL_HOST), $domain); | |
$sld = substr($domain[0], 0, strpos($domain[0], '.')); | |
$tld = substr($domain[0], strpos($domain[0], '.')); | |
switch ($segment) { | |
case ('all') : | |
case ('href') : | |
return $url; | |
break; | |
case ('protocol') : | |
case ('scheme') : | |
case ('schema') : | |
if ( $url_compontents['scheme'] != '' ) { | |
return $url_compontents['scheme']; | |
} else { | |
return false; | |
} | |
break; | |
case ('host') : | |
case ('hostname') : | |
return $url_compontents['host']; | |
break; | |
case ('www') : | |
if ( $host[0] == 'www' ) { | |
return 'www'; | |
} else { | |
return false; | |
} | |
break; | |
case ('subdomain') : | |
case ('sub_domain') : | |
if ( $host[0] != 'www' && $host[0] != $sld ) { | |
return $host[0]; | |
} else { | |
return false; | |
} | |
break; | |
case ('domain') : | |
return $domain[0]; | |
break; | |
case ('basedomain') : | |
case ('base_domain') : | |
case ('origin') : | |
return $url_compontents['scheme'] . '://' . $domain[0]; | |
break; | |
case ('sld') : //In example.com the sld is "example" | |
case ('second_level_domain') : | |
case ('second-level_domain') : | |
return $sld; | |
break; | |
case ('tld') : //In example.com the tld is ".com" | |
case ('top_level_domain') : | |
case ('top-level_domain') : | |
return $tld; | |
break; | |
case ('filepath') : | |
case ('pathname') : | |
return $url_compontents['path']; | |
break; | |
case ('file') : | |
case ('filename') : | |
if ( contains(basename($url_compontents['path']), array('.')) ) { | |
return basename($url_compontents['path']); | |
} else { | |
return false; | |
} | |
break; | |
case ('extension') : | |
if ( contains(basename($url_compontents['path']), array('.')) ) { | |
$file_parts = explode('.', $url_compontents['path']); | |
return $file_parts[1]; | |
} else { | |
return false; | |
} | |
break; | |
case ('path') : | |
if ( contains(basename($url_compontents['path']), array('.')) ) { //Note: This can possibly give bad data if the directory name has a "." in it | |
return str_replace(basename($url_compontents['path']), '', $url_compontents['path']); | |
} else { | |
return $url_compontents['path']; | |
} | |
break; | |
case ('query') : | |
case ('queries') : | |
case ('search') : | |
return $url_compontents['query']; | |
break; | |
default : | |
return $url; | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment