method
subdomains_from
v8.1.1 -
Show latest stable
- Class:
ActionDispatch::Http::URL::DomainExtractor
subdomains_from(host, tld_length)public
Extracts the subdomain components from a host string as an Array.
Returns all the components that come before the domain and TLD parts. The tld_length parameter is used to determine where the domain begins so that everything before it is considered a subdomain.
Parameters
- host
-
The host string to extract subdomains from.
- tld_length
-
The number of domain components that make up the TLD. This affects where the domain boundary is calculated.
Examples
# Standard TLD (tld_length = 1) DomainExtractor.subdomains_from("www.example.com", 1) # => ["www"] # Country-code TLD (tld_length = 2) DomainExtractor.subdomains_from("api.staging.example.co.uk", 2) # => ["api", "staging"] # No subdomains DomainExtractor.subdomains_from("example.com", 1) # => [] # Single subdomain with complex TLD DomainExtractor.subdomains_from("www.mysite.co.uk", 2) # => ["www"] # Multiple levels of subdomains DomainExtractor.subdomains_from("dev.api.staging.example.com", 1) # => ["dev", "api", "staging"]