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";

blnukemAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oleberCommented:
something like


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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SmartIntelCommented:
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";
}
0
mjcoyneCommented:
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?
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

oleberCommented:
someone is having problems with my fix?
is it to short or what?
0
TintinCommented:
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

0
TintinCommented:
oleber

I hope you realise that if you have a valid URL of http://www.example.com/ that you'll get an incorrect result.
0
mjcoyneCommented:
"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.
0
oleberCommented:
$_ = "home.htm" if not s|http://.+/(.+)|$1|;
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.