PHP: Get last directory name from path

后端 未结 8 1866
说谎
说谎 2021-01-11 10:52

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.

$q         


        
相关标签:
8条回答
  • 2021-01-11 11:24

    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];
    
    0 讨论(0)
  • 2021-01-11 11:27
    $qa_path=explode('/', '/learnphp/docs/');
    echo $qa_path[2]; // output docs
    
    0 讨论(0)
  • 2021-01-11 11:39

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

    0 讨论(0)
  • 2021-01-11 11:40

    This is the easiest way:

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

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

    0 讨论(0)
  • 2021-01-11 11:46

    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
    
    0 讨论(0)
  • 2021-01-11 11:47

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

    0 讨论(0)
提交回复
热议问题