php 如何从字符串中的路径获取最后一个目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4733851/
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 dir from a path in a string
提问by Rickstar
I am trying to get the last folder name from a path that i store in a string.
我试图从我存储在字符串中的路径中获取最后一个文件夹名称。
e.g:Home/new_folder/test
例如:Home/new_folder/test
result = test
回答by acm
Use basename
使用基名
basename('Home/new_folder/test');
// output: test
As a side note to those who answered explode:
作为回答爆炸的人的旁注:
To get the trailing name component of a path you should use basename!
In case your path is something like $str = "this/is/something/"
the end(explode($str));
combo will fail.
要获取路径的尾随名称组件,您应该使用basename!如果你的路径是这样$str = "this/is/something/"
的end(explode($str));
组合将失败。
回答by Kel
回答by Matt Lowden
You can use pathinfo - pathinfo
您可以使用 pathinfo - pathinfo
$pathinfo = pathinfo('dir/path', PATHINFO_DIRNAME);
$pathinfo = array_filter( explode('/', $pathinfo) );
$result = array_pop($pathinfo);
This will also make sure that a trailing slash doesn't mean a blank string is returned.
这也将确保尾部斜杠并不意味着返回空白字符串。
回答by Ash501
I know it's an old question, but this automatically gets the last folder without confusing the last item on the list - that could be the script - and not the actual last folder.
我知道这是一个老问题,但这会自动获取最后一个文件夹,而不会混淆列表中的最后一个项目——这可能是脚本——而不是实际的最后一个文件夹。
$url = pathinfo($_SERVER['SCRIPT_NAME'], PATHINFO_DIRNAME);
$url_var = explode('/' , $url);
$last_folder = end($url_var);
回答by Alan Whitelaw
Explode turns the string into an array, you can then choose the last value in that array to be your result.
Explode 将字符串转换为数组,然后您可以选择该数组中的最后一个值作为结果。
$result = end((explode('/', $path)));
回答by BradChesney79
So, you want something dynamic that will work most of the time as is-- maybe a reusable function or something.
所以,你想要一些动态的东西,大部分时间都可以正常工作——也许是一个可重用的函数或其他东西。
Get the URI from the data the webserver gave you in the request via the $_SERVER data: $_SERVER('REQUEST_URI')
从网络服务器通过 $_SERVER 数据为您提供的数据中获取 URI: $_SERVER('REQUEST_URI')
From that URI, get the path: parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH))
从该 URI,获取路径: parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH))
basename() is the right tool to get the last directory once you've distilled a path from the full URI: basename(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH))
basename() 是从完整 URI 中提取路径后获取最后一个目录的正确工具: basename(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH))
function lastPathDir() {
// get a URI, parse the path from it, get the last directory, & spit it out
return basename(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
}
回答by MikeStr
Try this
尝试这个
echo basename(dirname(__FILE__));
or this
或这个
echo basename(dirname(__DIR__));
回答by Shree Sthapit
You can do
你可以做
$baseUrl=basename('/path/to/site');
echo $baseUrl;
Incase your URL has '/'
at the end you can do:
如果您的网址'/'
末尾有您可以执行的操作:
$url_to_array = parse_url('/path/to/site/');
$baseUrl = basename($url_to_array['path']);
echo $baseUrl;`
回答by Bj?rn Kaiser
<?php
$path = explode('/', $yourPathVar);
// array_pop gives you the last element of an array()
$last = array_pop($path);
?>
回答by Mark Baker
$directory = 'Home/new_folder/test';
$path = explode('/',$directory);
$lastDir = array_pop($path);