PHP: Get last directory name from path

2020-03-12 04:00发布

I am writing one function for getting some different database query. Now things are going well but only need to get last directory name from defined path.

$qa_path=site_root('/learnphp/docs/');

I wan to get only docs from above path. Here site_root is nothing but $_SERVER['DOCUMENT_ROOT'] So how can I get only docs ?

Thanks

标签: php trim
8条回答
Melony?
2楼-- · 2020-03-12 04:26

Easiest way would be to use basename($yourpath) as you can see here: http://php.net/basename

查看更多
男人必须洒脱
3楼-- · 2020-03-12 04:28

Provided answer doesn't work if your string contains the file at the end, like :

basename('/home/mypath/test.zip');

gives

test.zip

So if your string contains the file, don't forget to dirname it first

basename(dirname('/home/mypath/test.zip'));

gives

mypath
查看更多
登录 后发表回答