Once You Know, You Newegg

Perl – Split URL

# This will split a url like: ‘http://jamesconzett.com/whatever/this’
# on each forward slash “/”.

#!/usr/bin/perl -w
use strict;
use CGI;
use CGI ':standard';
use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
print CGI::header();
use vars qw($url @splitURL $url $part);

$url=’http://jamesconzett.com/whatever/this’;
@splitURL = split(/\//, $url);

# Like:
print @splitURL[0]; # = http:
print @splitURL[1]; # = nothing
print @splitURL[2]; # = jamesconzett.com
print @splitURL[3]; # = whatever
print @splitURL[4]; # = this

# Or in a loop:
print “$_” for @splitURL;

# Or:
foreach (@splitURL){
print qq~$_~;
}

#Or:
foreach $part(@splitURL){
print qq~$part~;
}

Comments are closed.