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条回答
Evening l夕情丶
2楼-- · 2020-03-12 04:11

This will help you

$qa_path=site_root('/learnphp/docs/');
$q_path = explode ("/", $qa_path);
$lastV =  end($q_path);
查看更多
▲ chillily
3楼-- · 2020-03-12 04:12

This is the easiest way:

<?php
  echo basename(getcwd());
?>

getcwd() = give your full directory path basename() = give you last directory

查看更多
我想做一个坏孩纸
4楼-- · 2020-03-12 04:17
$qa_path=explode('/', '/learnphp/docs/');
echo $qa_path[2]; // output docs
查看更多
可以哭但决不认输i
5楼-- · 2020-03-12 04:17

This gives you the current directory name: echo basename(dirname(__FILE__));

查看更多
爷的心禁止访问
6楼-- · 2020-03-12 04:19

you can use this simple snippet:

$qa_path=site_root('/learnphp/docs/');
$qa_path = explode("/", $qa_path);
$qa_path = $qa_path[count($qa_path) - 1];
查看更多
ら.Afraid
7楼-- · 2020-03-12 04:22

Try explode('/', '/learnphp/docs/') to split the string into array locations. Then fetch the last location.

Here is more info: http://php.net/manual/en/function.explode.php

查看更多
登录 后发表回答