By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,982 Members | 1,955 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,982 IT Pros & Developers. It's quick & easy.

URLs

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

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 discussion thread is closed

Replies have been disabled for this discussion.