If I had an URL:
http://www.example.com/page
I would like to interpret that to:
example.com
But, if I had:
http://blog.example.com/page
I'd like to get back:
blog.example.com
Is that difficult?
Use Ruby's URI module:
require 'uri'
URI.parse('http://www.example.com/page').host
=> "www.example.com"
URI.parse('http://blog.example.com/page').host
=> "blog.example.com"
In both cases, URI extracts the whole host name, because selectively stripping the host from the domain makes no sense.
You'll have to implement that logic separately, using something like:
%w[http://www.example.com/page http://blog.example.com/page].each do |u|
puts URI.parse(u).host.sub(/^www\./, '')
end
Which outputs:
example.com
blog.example.com
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With