Link to home
Start Free TrialLog in
Avatar of blnukem
blnukem

asked on

Strip page name from URL

Hi All

I need to strip out the last page name in a variable.

IE, the variable my look like this.

$variable = http://www.mydomaim.com
$variable = http://www.mydomaim.com/result.htm
$variable = http://www.mydomaim.com/some_page1/result.htm
$variable = http://www.mydomaim.com/some_page1/some_page2/result.htm


Desired result:

$PageName = "result.htm";

But if $variable = http://www.mydomaim.com

Then the desired result is:

$PageName = "home.htm";

ASKER CERTIFIED SOLUTION
Avatar of oleber
oleber
Flag of Portugal image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can try in 2 ways

First match the last htm and assign value to  result.htm

if ($variable=~/.+\.htm$/)
{
$PageName="result.htm";
}
else {
$PageName="home.htm";
}


second method is
@eachelement=split/\//, $variable;
$length=@eachelement;
if ($length >3)
{
$PageName="result.htm";
}
else { $PageName="home.htm";
}
Avatar of mjcoyne
mjcoyne

I put the example URLs in an array to make this example easier:

#!/usr/bin/perl -w
use strict;

my @urls = ('http://www.mydomaim.com', 'http://www.mydomaim.com/result.htm',
'http://www.mydomaim.com/some_page1/result.htm', 'http://www.mydomaim.com/some_page1/some_page2/result.htm');

foreach my $variable (@urls) {
    my $PageName;
    my ($page) = ($variable =~ /http:\/\/.+\/(.+)$/);
    ($page) ? ($PageName = $page) : ($PageName = "home.htm");
    print "$PageName\n";
}

The output is:

home.htm
result.htm
result.htm
result.htm

Is that what you're looking for?
someone is having problems with my fix?
is it to short or what?
Avatar of Tintin
blnukem.

What result do you want for http://www.mydomaim.com/
Presumably that should result in home.htm, in which case, change oleber's suggestion to:




$variable= "home.htm" if not ($variable =~ s|http://.+/(\S+)|$1|);

Open in new window

oleber

I hope you realise that if you have a valid URL of http://www.example.com/ that you'll get an incorrect result.
"I hope you realise that if you have a valid URL of http://www.example.com/ that you'll get an incorrect result."

Not with mine...  But I noticed that defect in the others.
$_ = "home.htm" if not s|http://.+/(.+)|$1|;