开发者

How to get the last path in a URL?

I would 开发者_如何学Pythonlike 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 tried to use $_SERVER['REQUEST_URI'], but I get the whole URL path.


Try:

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

Assuming $tokens has at least 2 elements.


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


it is easy

<?php
 echo basename(dirname($url)); // if your url/path includes a file
 echo basename($url); // if your url/path does not include a file
?>
  • basename will return the trailing trailing name component of path
  • dirname will return the parent directory's path

http://php.net/manual/en/function.dirname.php

http://php.net/manual/en/function.basename.php


Try this:

 $parts = explode('/', 'your_url_here');
 $last = end($parts);


$arr =  explode("/", $uri);


Another solution:

$last_slash = strrpos('/', $url);
$last = substr($url, $last_slash);

1: getting the last slash position 2: getting the substring between the last slash and the end of string

Look here: TEST


If you want to process an absolute URL, then you can use parse_url() (it doesn't work with relative urls).

$url = 'http://aplicaciones.org/wp-content/uploads/2011/09/skypevideo-500x361.jpg?arg=value#anchor';
print_r(parse_url($url));
$url_path = parse_url($url, PHP_URL_PATH);
$parts = explode('/', $url_path);
$last = end($parts);
echo $last;

Full code example here: http://codepad.org/klqk5o29


I wrote myself a little function to get the last dir/folder of an url. It only works with real/existing urls, not theoretical ones. In my case, that was always the case, so ...

function uf_getLastDir($sUrl)
{
    $sPath = parse_url($sUrl, PHP_URL_PATH);   // parse URL and return only path component 
    $aPath = explode('/', trim($sPath, '/'));  // remove surrounding "/" and return parts into array
    end($aPath);                               // last element of array
    if (is_dir($sPath))                        // if path points to dir
        return current($aPath);                // return last element of array
    if (is_file($sPath))                       // if path points to file
        return prev($aPath);                   // return second to last element of array
    return false;                              // or return false
}

Works for me! Enjoy! And kudos to the previous answers!!!


This will keep the part after the last slash.
No worries about explode, when for example no slash is there.

$url = 'http://blabla/blablabla/dut2a/news.php';
$url = preg_replace('~.*/~', '', $url);

Will give

news.php
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜