Usage
nebula()->url_components($segment, $url)
Parameters
$segment
(Optional) (String) What segment of data to return
Default: "all"
$url
(Optional) (String) Check a specific URL
Default: Current URL
Parameter Notes
$segment string can include “all”, “protocol”, “scheme”, “port”, “user”, “pass”, “authority”, “hostname”, “www”, “subdomain”, “domain”, “basedomain”, “origin”, “sld”, “tld”, filepath”, “filename”, “extension”, “path”, “query”, “hash”, and variations of those.
Passing “all” or “href” (or leaving it empty) will return the full URL.
Examples
<?php echo nebula()->url_components('filename'); ?>
Combine with Advanced Custom Fields to detect segments of a user-entered URL.
<?php echo nebula()->url_components('tld', get_field('url')); ?>
Demo
Nebula Requested URL
https://nebula.gearside.com/functions/url_components/
Detected URL
Segment | Return Value |
all (default) | https://nebula.gearside.com/functions/url_components/ |
protocol | https |
user | |
pass | |
authority | |
port | 443 |
host | nebula.gearside.com |
www | |
subdomain | nebula |
domain | gearside.com |
basedomain | https://gearside.com |
sld | gearside |
tld | .com |
filepath | /functions/url_components/ |
path | /functions/url_components/ |
file | |
extension | |
query | |
fragment |
Passed URL
https://something.gearside.co.uk/nebula/documentation/custom-functionality/nebula-url-components/filename.php?query=something#anchorpoint
Segment | Return Value |
all (default) | https://something.gearside.co.uk/nebula/documentation/custom-functionality/nebula-url-components/filename.php?query=something#anchorpoint |
protocol | https |
user | |
pass | |
authority | |
port | 443 |
host | something.gearside.co.uk |
www | |
subdomain | something |
domain | gearside.co.uk |
basedomain | https://gearside.co.uk |
sld | gearside |
tld | .co.uk |
filepath | /nebula/documentation/custom-functionality/nebula-url-components/filename.php |
path | /nebula/documentation/custom-functionality/nebula-url-components/ |
file | filename.php |
extension | php |
query | query=something |
fragment | anchorpoint |
Relative URL
/var/home/public_html/assets/img/animals/dogs/corgi.png
Segment | Return Value |
all (default) | /var/home/public_html/assets/img/animals/dogs/corgi.png |
protocol | |
user | |
pass | |
authority | |
port | |
host | |
www | |
subdomain | |
domain | |
basedomain | |
sld | |
tld | |
filepath | /var/home/public_html/assets/img/animals/dogs/corgi.png |
path | /var/home/public_html/assets/img/animals/dogs/ |
file | corgi.png |
extension | png |
query | |
fragment |
URL Tester
Enter a URL to see what nebula()->url_components()
returns.
Source File
Located in /libs/Utilities/Utilities.php on line 441.
1 Hook
Find these filters and actions in the source code below to hook into them. Use do_action() and add_filter() in your functions file or plugin.
Filters
"pre_nebula_url_components"Need a new filter hook? Request one here.
Actions
This function has no action hooks available. Request one?Note: This function contains 5 to-do comments.
public function url_components($segment="all", $url=null){ $override = apply_filters('pre_nebula_url_components', null, $segment, $url); if ( isset($override) ){return $override;} //If URL is not passed, get the current page URL. //@todo "Nebula" 0: Use null coalescing operator here if ( !$url ){ $url = $this->requested_url(); } //If it is not a valid URL, treat it as a relative path $relative = false; if ( !filter_var($url, FILTER_VALIDATE_URL) ){ $relative = true; $url = 'http://example.com' . $url; //Prepend it with a temporary protocol, SLD, and TLD so it can be parsed (and removed later). } $url_components = parse_url(html_entity_decode($url)); if ( empty($url_components['host']) ){ return; } $host = explode('.', $url_components['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); if ( !empty($domain) ){ $sld = substr($domain[0], 0, strpos($domain[0], '.')); $tld = substr($domain[0], strpos($domain[0], '.')); } switch ($segment){ case ('all'): case ('href'): return str_replace('http://example.com', '', $url); case ('protocol'): //Protocol and Scheme are aliases and return the same value. case ('scheme'): //Protocol and Scheme are aliases and return the same value. case ('schema'): if ( $relative ){ return false; } if ( isset($url_components['scheme']) ){ return $url_components['scheme']; } return false; case ('port'): if ( $relative ){ return false; } if ( isset($url_components['port']) ){ return $url_components['port']; } switch( $url_components['scheme'] ){ case ('http'): return 80; //Default for http case ('https'): return 443; //Default for https case ('ftp'): return 21; //Default for ftp case ('ftps'): return 990; //Default for ftps default: return false; } case ('user'): //Returns the username from this type of syntax: https://username:[email protected]/ case ('username'): if ( $relative ){ return false; } if ( isset($url_components['user']) ){ return $url_components['user']; } return false; case ('pass'): //Returns the password from this type of syntax: https://username:[email protected]/ case ('password'): if ( $relative ){ return false; } if ( isset($url_components['pass']) ){ return $url_components['pass']; } return false; case ('authority'): if ( $relative ){ return false; } if ( isset($url_components['user'], $url_components['pass']) ){ return $url_components['user'] . ':' . $url_components['pass'] . '@' . $url_components['host'] . ':' . $this->url_components('port', $url); } return false; case ('host'): //In http://something.example.com the host is "something.example.com" case ('hostname'): if ( $relative ){ return false; } if ( isset($url_components['host']) ){ return $url_components['host']; } return false; case ('www') : if ( $relative ){ return false; } if ( $host[0] === 'www' ){ return 'www'; } return false; case ('subdomain'): case ('sub_domain'): if ( $relative ){ return false; } if ( $host[0] !== 'www' && $host[0] !== $sld ){ return $host[0]; } return false; case ('domain') : //In http://example.com the domain is "example.com" if ( $relative ){ return false; } if ( isset($domain[0]) ){ return $domain[0]; } return false; case ('basedomain'): //In http://example.com/something the basedomain is "http://example.com" case ('base_domain'): case ('origin') : if ( $relative ){ return false; } if ( isset($url_components['scheme']) ){ return $url_components['scheme'] . '://' . $domain[0]; } return false; case ('sld') : //In example.com the sld is "example" case ('second_level_domain'): case ('second-level_domain'): if ( $relative ){ return false; } return $sld; case ('tld') : //In example.com the tld is ".com" case ('top_level_domain'): case ('top-level_domain'): if ( $relative ){ return false; } return $tld; case ('filepath'): //Filepath will be both path and file/extension case ('pathname'): if ( isset($url_components['path']) ){ return $url_components['path']; } return false; case ('file'): //Filename will be just the filename/extension. case ('filename'): if ( strpos(basename($url_components['path']), '.') !== false ){ //@todo "Nebula" 0: Update strpos() to str_contains() in PHP8 return basename($url_components['path']); } return false; case ('type'): case ('filetype'): case ('extension'): //Only the extension (without ".") if ( strpos(basename($url_components['path']), '.') !== false ){ //@todo "Nebula" 0: Update strpos() to str_contains() in PHP8 $file_parts = explode('.', $url_components['path']); return $file_parts[count($file_parts)-1]; } return false; case ('path'): //Path should be just the path without the filename/extension. if ( strpos(basename($url_components['path']), '.') !== false ){ //@TODO "Nebula" 0: This will possibly give bad data if the directory name has a "." in it //@todo "Nebula" 0: Update strpos() to str_contains() in PHP8 return str_replace(basename($url_components['path']), '', $url_components['path']); } return $url_components['path']; case ('query'): case ('queries'): case ('search'): if ( isset($url_components['query']) ){ return $url_components['query']; } return false; case ('fragment'): case ('fragments'): case ('anchor'): case ('hash') : case ('hashtag'): case ('id'): if ( isset($url_components['fragment']) ){ return $url_components['fragment']; } return false; default : return $url; } }
Override
To override this PHP function, use this hook in your child theme or plugin ("my_custom" can be changed):
add_filter('pre_nebula_url_components', 'my_custom_url_components', 10, 3); //The last integer must be 1 more than the actual parameters function my_custom_url_components($null, $segment, $url){ //$null is required, but can be ignored //Write your own code here return true; //Return true to prevent the original function from running afterwords }
You can completely disable this PHP function with a single line actions:
add_filter('pre_nebula_url_components', '__return_false');