php 如何获取 URL 中的最后一个路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2273280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the last path in a URL?
提问by bahamut100
I would like get the last path segment in a URL:
我想获取 URL 中的最后一个路径段:
http://blabla/bla/wce/news.phporhttp://blabla/blablabla/dut2a/news.php
http://blabla/bla/wce/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 路径。
回答by Bart Kiers
Try:
尝试:
$url = 'http://blabla/blablabla/dut2a/news.php';
$tokens = explode('/', $url);
echo $tokens[sizeof($tokens)-2];
Assuming $tokenshas at least 2 elements.
假设$tokens至少有 2 个元素。
回答by alex
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
回答by Stepan Suvorov
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
?>
basenamewill return the trailing trailing name component of pathdirnamewill return the parent directory's path
basename将返回路径的尾随名称部分dirname将返回父目录的路径
http://php.net/manual/en/function.dirname.php
http://php.net/manual/en/function.dirname.php
回答by Sarfraz
Try this:
尝试这个:
$parts = explode('/', 'your_url_here');
$last = end($parts);
回答by Briganti
回答by julianm
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
回答by Gabe
$arr = explode("/", $uri);
回答by maxpower9000
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!!!
为我工作!享受!并对之前的答案表示敬意!!!

