• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 247
  • Last Modified:

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

0
blnukem
Asked:
blnukem
  • 3
  • 2
  • 2
  • +1
1 Solution
 
oleberCommented:
something like


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

0
 
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
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now