views:

334

answers:

6

Hi, I would like get the last path segment in a URL:

  • http://blabla/bla/wce/news.php or
  • http://blabla/blablabla/dut2a/news.php

For example, in these two URLs, I want to get the path segment: 'wce', and 'dut2a'.

I try to use $_SERVER['REQUEST_URI'], but I get the whole URL path.

A: 

use explode function

Ilya Biryukov
+3  A: 

Try:

$url = 'http://blabla/blablabla/dut2a/news.php';
$tokens = explode('/', $url);
echo $tokens[sizeof($tokens)-2];

Assuming $tokens has at least 2 elements.

Bart Kiers
Thanks, It's that I want :)
bahamut100
bahamut100, If this is the right answer, then you should accept it by clicking the white checkmark next to it.
Mike Sherov
@bahamut100, you're welcome. Although I'd go for (and accept) alex' answer.
Bart Kiers
@Bart K. It's OK, yours will get the job done too! +1
alex
It will not handhe: http:/ /foobar.com/path/path#fragment/fragment
ddimitrov
@ddimitrov: yes, good point. The more reason for the OP to go with Alex' solution. Can't remove my answer though since it is the accepted answer...
Bart Kiers
A: 
$arr =  explode("/", $uri);
gmcalab
+4  A: 

Try this:

     function getLastPathSegment($url) {
        $path = parse_url($url, PHP_URL_PATH); // to get the path from a whole URL
        $pathTrimmed = trim($path, '/'); // normalise with no leading or trailing slash
        $pathTokens = explode('/', $pathTrimmed); // get segments delimited by a slash

        if (substr($path, -1) !== '/') {
            array_pop($pathTokens);
        }
        return end($pathTokens); // get the last segment
   }

    echo getLastPathSegment($_SERVER['REQUEST_URI']);

I've also tested it with a few URLs from the comments. I'm going to have to assume that all paths end with a slash, because I can not identify if /bob is a directory or a file. This will assume it is a file unless it has a trailing slash too.

echo getLastPathSegment('http://server.com/bla/wce/news.php'); // wce

echo getLastPathSegment('http://server.com/bla/wce/'); // wce

echo getLastPathSegment('http://server.com/bla/wce'); // bla
alex
+1 For using `parse_url` to get just the URL path. But you need to do that with `$_SERVER['REQUEST_URI']` as well.
Gumbo
I thought `$_SERVER['REQUEST_URI']` always came back with `something/like/this` ?
alex
@alex: No, in PHP it does also contain the query. So URL path and URL query (as in the HTTP request line).
Gumbo
@gumbo Thanks for the tip, I've made a function wrapper for it too.
alex
in something like this: `/bla/wce/news.php` wouldn't it return `news.php` instead of `wce` like the OP wanted? Shouldn't it returns the second last element instead of end()? I didn't really test this as it requires PHP version >= 5.1.2 so cmiiw.
andyk
@andyk Thanks for dropping by. I've made some modifications to the function, and run your string and some similar through for their respective outputs.
alex
A: 

Try this:

 $parts = explode('/', 'your_url_here');
 $last = end($parts);
Sarfraz
+3  A: 

it is easy

<?php
 echo basename(dirname($url));
?>
STEVER
could have use some filtering on the url before that. Will fail on something like this `http://www.example.com/dir1/dir2/foo?redirect=http://anothersite.com/default.aspx`
andyk