Needed only the Domain name from an url

S

Srikanth Jeeva

A

Andrew Timberlake

hi,
Suppose if i give,
url="www.google.com/picasa/images/1"

I need only the string "google" which is the domain name.

right now i have tried this,

require 'rubygems'
require 'uri'
url = "http://www.google.com/picasa/images/1"
puts url = URI.parse(url).host
-----------
output: "google.com"
-----------
but i want only the word "google". please help.

Pts:
the domain name can also be, "google.co.in"
ie) for example, "http://www.google.co.in/picasa/images/1"

Thanks in advance,
srikanth


Technically google.com is the domain
If I run your code, I get "www.google.com" as the output, not "google.com"
This poses an interesting problem because a domain can be:
one.two.three.four.google.co.uk
What is the domain for you in that?

Otherwise you could do something like
uri.host.sub(/.*?\.?([^.]+)(?:\.\w{3}|\.\w{2,3}\.\w{2,3})$/, '\1')

It may not be the prettiest regex but it gets the job done for the following:
* one.two.google.com
* one.two.google.co.in
* www.somesite.com.au
* www.one.co.uk
but it won't work for
* www.one.com (but it will work for one.com)

Andrew Timberlake
http://ramblingsonrails.com
http://www.linkedin.com/in/andrewtimberlake

"I have never let my schooling interfere with my education" - Mark Twain
 
R

Raveendran Perumalsamy

Srikanth said:
hi,
Suppose if i give,
url="www.google.com/picasa/images/1"

I need only the string "google" which is the domain name.

right now i have tried this,

require 'rubygems'
require 'uri'
url = "http://www.google.com/picasa/images/1"
puts url = URI.parse(url).host
-----------
output: "google.com"
-----------
but i want only the word "google". please help.

Pts:
the domain name can also be, "google.co.in"
ie) for example, "http://www.google.co.in/picasa/images/1"

Thanks in advance,
srikanth



Hi Sri,

Are you got the solution ?

If Yes, then update here about it..

Regards
P.Raveendran
http://raveendran.wordpress.com
 
K

kranthi reddy

[Note: parts of this message were removed to make it a legal post.]

If you are using a rails app then something similar to this can be done:

In the application controller
private

def locate_subdomain
domain = request.domain # for just the domain
name
subdomain = request.subdomain # for the
sudomain
 
K

kranthi reddy

[Note: parts of this message were removed to make it a legal post.]

If you are using a rails app then something similar to this can be done:

In the application controller
private

def locate_subdomain

end
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top