How to get the last dir from a path in a string

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

e.g: Home/new_folder/test

result = test

Use basename

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

As a side note to those who answered explode:

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.


You can use basename() function:

$last = basename("Home/new_folder/test");

You can use pathinfo - pathinfo

$pathinfo = pathinfo('dir/path', PATHINFO_DIRNAME);
$pathinfo = array_filter( explode('/', $pathinfo) );

$result = array_pop($pathinfo);

This will also make sure that a trailing slash doesn't mean a blank string is returned.