如何从字符串路径中获取最后一个目录 [英] How to get the last dir from a path in a string

查看:177
本文介绍了如何从字符串路径中获取最后一个目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我存储在字符串中的路径中获取最后一个文件夹名称.

I am trying to get the last folder name from a path that i store in a string.

例如: Home/new_folder/test

result = test

推荐答案

使用基本名称

basename('Home/new_folder/test');
// output: test

作为回答者的补充说明会爆炸:

As a side note to those who answered explode:

要获取路径的结尾名称部分,您应该使用 basename ! 如果您的路径类似于$str = "this/is/something/",则end(explode($str));组合将失败.

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.

这篇关于如何从字符串路径中获取最后一个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆