Extract domain name

Discussion in 'Perl' started by Shabam, Nov 12, 2004.

  1. Shabam

    Shabam Guest

    How do you fetch just the domain name part of a variable in a script? The
    variable can be "http://www.domain.com/blahblah/whatever/page.htm" or
    "http://sub.domain.com/blahblah/whatever/page.htm".

    What I need is to extract just the "domain.com".
    Shabam, Nov 12, 2004
    #1
    1. Advertising

  2. * Shabam:
    > How do you fetch just the domain name part of a variable in a script? The
    > variable can be "http://www.domain.com/blahblah/whatever/page.htm" or
    > "http://sub.domain.com/blahblah/whatever/page.htm".
    >
    > What I need is to extract just the "domain.com".


    try something like:

    my(undef, undef, $domain) = $s =~
    /^(http|ftp|whatever):\/\/(www|ftp|whatever)\.(.+?)\//;

    There might be some module that does what you're looking for (check
    CPAN), I didn't look for it, actually...

    bye
    //Dave

    --
    Ja, der typische "Ich tippe alles ein was mir ein paar Trottel aus dem
    IRC sagen"-Newbie wird aber kein Solaris nutzen... ;)
    -- Christoph Gebhardt in bjt
    David Kroeber, Nov 12, 2004
    #2
    1. Advertising

  3. Look for URI module. IMHO, its a good and simple thing for parsing URLs

    use URI;
    ($domain = URI->new("http://www.domain.com/blahblah/whatever/page.htm")->authority) =~ s/^www\.//i


    Regards,
    Andrew

    Shabam wrote on 12 ÐоÑбрь 2004 16:02:

    > How do you fetch just the domain name part of a variable in a script? The
    > variable can be "http://www.domain.com/blahblah/whatever/page.htm" or
    > "http://sub.domain.com/blahblah/whatever/page.htm".
    >
    > What I need is to extract just the "domain.com".


    --
    Andrew
    Andrew Tkachenko, Nov 12, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joey
    Replies:
    0
    Views:
    336
  2. Berlin  Brown
    Replies:
    9
    Views:
    25,750
    Nigel Wade
    Mar 21, 2006
  3. Chem Leakhina
    Replies:
    2
    Views:
    129
    Robert Klemme
    Jun 23, 2009
  4. Charles Calvert

    Extract domain name

    Charles Calvert, Aug 20, 2010, in forum: Ruby
    Replies:
    5
    Views:
    138
    Charles Calvert
    Aug 23, 2010
  5. Shabam

    Extract domain name

    Shabam, Nov 12, 2004, in forum: Perl Misc
    Replies:
    6
    Views:
    102
Loading...

Share This Page