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
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];
$qa_path=explode('/', '/learnphp/docs/');
echo $qa_path[2]; // output docs
Easiest way would be to use basename($yourpath)
as you can see here: http://php.net/basename
This is the easiest way:
<?php
echo basename(getcwd());
?>
getcwd() = give your full directory path basename() = give you last directory
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
This gives you the current directory name:
echo basename(dirname(__FILE__));