CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
How to substract a part of url ?

 



adgort
New User

Jun 14, 2010, 5:17 AM

Post #1 of 2 (588 views)
How to substract a part of url ? Can't Post

I have a little problem to get only the FQDN from url string.
My url is always like http://www.domain.com/something
so this is relatively simple to substract first "http://www." and then everything after domain name extension.

But some of my urls are based on canonical subdomains like the following:

http://subdomain.domain.com/something and as subdomain is never the same string, I cannot imagine, how would be possible to get only "domain.com" from such kind of url string ?
Thanks in advance for any clues ;)


BillKSmith
Veteran

Jun 14, 2010, 11:40 AM

Post #2 of 2 (581 views)
Re: [adgort] How to substract a part of url ? [In reply to] Can't Post

My RE solves the given problem. There are almost certainly valid URLs that it would not parse correctly. For serious work, I would recommend searching CPAN for a URL parser.




Code
  

use strict;
use warnings;
my @urls = (
"http://subdomain.domain.com/something",
"http://domain.com/something",
"http://www.domain.com/something",
"www.domain.com",
"domain.com",
);

foreach my $url (@urls) {

(my $FQDN) = $url =~ /(?:http:\/\/)?([^.]+\.\w{1,3})(?:\/\w*)?$/;
print $FQDN, "\n";
}

Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives