[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

String get domain name from web address

John Butler

7/9/2008 8:52:00 AM

Hi,

I could have a any web address, www.yahoo.com, http://...,
http://www...., http://www.ya... so i want i need is some
ruby code that will always return the domain name "yahoo.com"

Im not sure what the best way to this as the domain name isnt always in
the same format.

Anyone got some ideas on how to get this as accuarte as possible??

JB
--
Posted via http://www.ruby-....

2 Answers

Dave Bass

7/9/2008 11:31:00 AM

0

John Butler wrote:
> Hi,
>
> I could have a any web address, www.yahoo.com, http://...,
> http://www...., http://www.ya... so i want i need is some
> ruby code that will always return the domain name "yahoo.com"
>
> Im not sure what the best way to this as the domain name isnt always in
> the same format.
>
> Anyone got some ideas on how to get this as accuarte as possible??
>
> JB

For the examples you've shown, doing a regexp match on "yahoo.co" would
pick up all of them.

Note that x.com and x.co.uk are two separate domains, and might be
completely unconnected with each other. If you want to consider them as
the same domain, it would be better to detect them separately, then map
one to the other using a lookup table (Ruby hash).

Dave
--
Posted via http://www.ruby-....

Lloyd Linklater

7/9/2008 2:39:00 PM

0

John Butler wrote:
> Hi,
>
> I could have a any web address, www.yahoo.com, http://...,
> http://www...., http://www.ya... so i want i need is some
> ruby code that will always return the domain name "yahoo.com"
>
> Im not sure what the best way to this as the domain name isnt always in
> the same format.
>
> Anyone got some ideas on how to get this as accuarte as possible??
>
> JB

First. You could have one bit that removes the http:// or http://www.

Then, go to the first / and remove from there to the end of the string.

That would leave you with what you are seeking, I think.
--
Posted via http://www.ruby-....