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

发布于 2024-08-21 22:12:15 字数 279 浏览 10 评论 0原文

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

  • http://blabla/bla/wce/news.php
  • http://blabla/blablabla/dut2a/news.php< /code>

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

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

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 tried to use $_SERVER['REQUEST_URI'], but I get the whole URL path.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

紧拥背影 2024-08-28 22:12:16

试试这个:

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']);

我还使用评论中的一些 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

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
゛时过境迁 2024-08-28 22:12:16

很容易

<?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 将返回路径的尾随名称组件
  • dirname 将返回父目录的路径

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

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

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

风吹过旳痕迹 2024-08-28 22:12:16

试试这个:

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

Try this:

 $parts = explode('/', 'your_url_here');
 $last = end($parts);
只涨不跌 2024-08-28 22:12:16
$arr =  explode("/", $uri);
$arr =  explode("/", $uri);
仙女 2024-08-28 22:12:16

另一种解决方案:

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

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

看这里:TEST

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

故笙诉离歌 2024-08-28 22:12:16

如果你想处理绝对URL,那么你可以使用 parse_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;

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

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

左岸枫 2024-08-28 22:12:16

我自己写了一个小函数来获取 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
}

对我有用!享受!并对之前的答案表示敬意!

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

绅刃 2024-08-28 22:12:16

这将保留最后一个斜杠之后的部分。
不用担心爆炸,例如当没有斜线时。

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

会给

news.php

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
只是偏爱你 2024-08-28 22:12:15

尝试:

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

假设 $tokens 至少有 2 个元素。

Try:

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

Assuming $tokens has at least 2 elements.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文