473,394 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

URLs

How may I capture the last /.../ in a url?

eg.. www.foo.com/foo/bar/baz/index.php?param=1

I want to capture 'baz'.

Thanks
Jun 17 '06 #1
4 1803
Use parse_url() function : http://www.php.net/function.parse-url

$url = www.foo.com/foo/bar/baz/index.php?param=1
$url_arr = explode ("//", parse_url($url))

$i = count($url_arr) - 1;
$last_dir = $url_arr[$i];

Not tested yet, but i think its work.

--
http://blog.deshot.com
http://www.cookdojo.com

J Huntley Palmer wrote:
How may I capture the last /.../ in a url?

eg.. www.foo.com/foo/bar/baz/index.php?param=1

I want to capture 'baz'.

Thanks


Jun 17 '06 #2
J Huntley Palmer wrote:
How may I capture the last /.../ in a url?

eg.. www.foo.com/foo/bar/baz/index.php?param=1

I want to capture 'baz'.

Thanks


Start from the end of the string and work backwards until you hit the
second /. Untested code follows:

$flag = false;
for ($i = strlen($url); $i > 0; $i--)
{
if ($url{$i} == '/')
{
if ($flag == true)
{
/* we have our match */
$url = substr($url, $i);
break;
}

/* got the first / */
$flag = true;
}
}
Jun 17 '06 #3
Joe Estock wrote:
J Huntley Palmer wrote:
How may I capture the last /.../ in a url?

eg.. www.foo.com/foo/bar/baz/index.php?param=1

I want to capture 'baz'.

Thanks

Start from the end of the string and work backwards until you hit the
second /. Untested code follows:

$flag = false;
for ($i = strlen($url); $i > 0; $i--)
{
if ($url{$i} == '/')
{
if ($flag == true)
{
/* we have our match */
$url = substr($url, $i);
break;
}

/* got the first / */
$flag = true;
}
}


Whoops, misread your intent. Corrections follow (again, untested).

$flag = false;
$j = 0;
for ($i = strlen($url); $i > 0; $i--)
{
if ($url{$i} == '/')
{
if ($flag == true)
{
/* we have our match */
$url = substr($url, $i, ($j - $i));
break;
}

/* got the first / */
$flag = true;
$j = $i;
}
}
Jun 17 '06 #4

J Huntley Palmer wrote:
How may I capture the last /.../ in a url?

eg.. www.foo.com/foo/bar/baz/index.php?param=1

I want to capture 'baz'.

Thanks


Just my $0.02:

$t = parse_url('www.foo.com/foo/bar/baz/index.php?param=1');
$t = pathinfo($t['path']);
$t = explode('/', $t['dirname']);
$t = array_pop($t);
var_dump('baz' == $t);

Sincerely,
Alexander
http://www.alexatnet.com/

Jun 17 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: phpkid | last post by:
Howdy I've been given conflicting answers about search engines picking up urls like: http://mysite.com/index.php?var1=1&var2=2&var3=3 Do search engines pick up these urls? I've been considering...
26
by: Howard Brazee | last post by:
I would like to click on a URL of a html document that will open several URLs at once for me. Does someone have an example of a html document that will do this?
1
by: DM | last post by:
I'm working on a site with more than 1700 HTML files. We'll be moving files around on this site a lot because we're reorganizing it. I'm thinking of writing a script that will convert all URLs in...
7
by: AES | last post by:
Encountered a URL containing a comma the other day -- the first time I've ever noticed that, so far as I can recall. It worked fine, however, and I gather commas are legal in URLs. Out of...
10
by: david | last post by:
Hi, all: I need a help from you about DataGrid control. I created a DataGrid, dg, in design view of .NET visual Stadio and use the builder to add a Hyperlink column to dg. I want to try to assign...
10
by: jflash | last post by:
Hello all, I feel dumb having to ask this question in the first place, but I just can not figure it out. I am wanting to set my site up using dynamic urls (I'm assuming that's what they're...
9
by: Salve =?iso-8859-1?Q?H=E5kedal?= | last post by:
What is the best regular expression for finding urls in plain text files? (By urls I mean http://www.something.com, but also www.something.com, or salve@somewhere.com) Salve
2
by: Simon Wigzell | last post by:
I have inherited a database driven website that comes with a table of image links. The images are scattered all of the internet and there are thousands of them. I would like to write an asp script...
3
by: WebCM | last post by:
How to apply nice URL-s into CMS? 1. Should we use nice urls for every page? 2. Do we need to put a FULL path into <a href="">? 3. What is faster and better? a) 10 rules in .htaccess...
4
by: Guy Macon | last post by:
As a personal learning experience with limited practical use, I have been doing some experiments with using .htaccess to redirect mis-typed URLs to a preferred canonical form. I have set up a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.