#!/usr/bin/perl -w
use strict;
my $URK = 'http:/://blah.foo.comwhatever/dir/file.extensionelarocko?query=blah&fckj=ekjl';
my $last_slash = rindex $URK, '/';
my $last_dot = rindex $URK, '.';
my $query = rindex $URK, '?';
print "$URK\n\n";
if( ($last_slash >= 0) and ($last_dot >= 0) )
{
printf "%35.35s: %s\n\n",
"looks like the file name is",
substr( $URK, $last_slash + 1);
printf "%35.35s: %s\n\n",
"and the extension is",
substr($URK,$last_dot + 1);
}
else
{
print "seems like we got index.something on our hands\n\n";
}
if($query >= 0)
{
printf "%35.35s: %s\n\n",
"We even got a query string, whoa",
substr($URK, $query + 1);
printf "%35.35s: %s\n\n",
"so the true filename would be",
substr ( $URK , $last_slash + 1, $query - $last_slash );
printf "%35.35s: %s\n\n",
"and the true file extension would be",
substr ( $URK , $last_dot + 1, $query - $last_dot - 1 );
}
__END__
=head1 RESULTS
http:/://blah.foo.comwhatever/dir/file.extensionelarocko?query=blah&fckj=ekjl
looks like the file name is: file.extensionelarocko?query=blah&fckj=ekjl
and the extension is: extensionelarocko?query=blah&fckj=ekjl
We even got a query string, whoa: query=blah&fckj=ekjl
so the true filename would be: file.extensionelarocko?
and the true file extension would b: extensionelarocko
=cut
####
## AND IF YOU WANNA CHECK FOR A VALID URL, YOU REALLY NEED A MODULE (URL::URI)
## BUT substr and rindex are still the best for the job
my $url = 'proto://domain.something/dir/file.extension';
my $protocol = substr $url, 0, index($url, '://'),'';
## yada yada yada, you get the point
####
my $url = 'ptoto://foo.combarz.erk/file.ext?query';
my ($proto, $domain, $filedirquery) =
$url =~
m|(\w{2,6})://([.a-zA-Z0-9-]+/)(.*?)$|;
print "($proto, $domain, $filedirquery)\n";