如何获取URL中的最后一个路径?

时间:2022-08-11 17:05:50

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

我想获取URL中的最后一个路径段:

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

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

例如,在这两个URL中,我想获得路径段:'wce'和'dut2a'。

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

我尝试使用$ _SERVER ['REQUEST_URI'],但我得到了整个URL路径。

9 个解决方案

#1


48  

Try:

尝试:

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

Assuming $tokens has at least 2 elements.

假设$令牌至少有2个元素。

#2


25  

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.

我还用评论中的几个URL测试了它。我将不得不假设所有路径都以斜杠结尾,因为我无法识别/ bob是目录还是文件。这将假设它是一个文件,除非它也有一个尾部斜杠。

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

#3


21  

it is easy

这很容易

<?php
 echo basename(dirname($url));
?>

#4


9  

Try this:

尝试这个:

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

#5


1  

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

1:获取最后一个斜杠位置2:获取最后一个斜杠和字符串结尾之间的子字符串

Look here: TEST

看这里:测试

#6


1  

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

如果要处理绝对URL,则可以使用parse_url()(它不适用于相对URL)。

$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

完整的代码示例:http://codepad.org/klqk5o29

#7


0  

use explode function

使用爆炸功能

#8


0  

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

#9


0  

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 ...

我写了一个小函数来获取url的最后一个目录/文件夹。它只适用于实际/现有网址,而不是理论网址。就我而言,情况总是如此,所以......

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!!!

适合我!请享用!并赞赏以前的答案!!!

#1


48  

Try:

尝试:

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

Assuming $tokens has at least 2 elements.

假设$令牌至少有2个元素。

#2


25  

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.

我还用评论中的几个URL测试了它。我将不得不假设所有路径都以斜杠结尾,因为我无法识别/ bob是目录还是文件。这将假设它是一个文件,除非它也有一个尾部斜杠。

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

#3


21  

it is easy

这很容易

<?php
 echo basename(dirname($url));
?>

#4


9  

Try this:

尝试这个:

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

#5


1  

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

1:获取最后一个斜杠位置2:获取最后一个斜杠和字符串结尾之间的子字符串

Look here: TEST

看这里:测试

#6


1  

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

如果要处理绝对URL,则可以使用parse_url()(它不适用于相对URL)。

$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

完整的代码示例:http://codepad.org/klqk5o29

#7


0  

use explode function

使用爆炸功能

#8


0  

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

#9


0  

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 ...

我写了一个小函数来获取url的最后一个目录/文件夹。它只适用于实际/现有网址,而不是理论网址。就我而言,情况总是如此,所以......

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!!!

适合我!请享用!并赞赏以前的答案!!!