Rickstar
Rickstar

Reputation: 6199

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

Upvotes: 66

Views: 61767

Answers (11)

Shree Sthapit
Shree Sthapit

Reputation: 316

You can do

$baseUrl=basename('/path/to/site');
echo $baseUrl;

Incase your URL has '/' at the end you can do:

$url_to_array = parse_url('/path/to/site/');
$baseUrl = basename($url_to_array['path']);
echo $baseUrl;`

Upvotes: 4

Nomikos Strigkos
Nomikos Strigkos

Reputation: 151

Try this

echo basename(dirname(__FILE__));

or this

echo basename(dirname(__DIR__));

Upvotes: 7

acm
acm

Reputation: 6647

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.

Upvotes: 143

BradChesney79
BradChesney79

Reputation: 678

So, you want something dynamic that will work most of the time as is-- maybe a reusable function or something.

Get the URI from the data the webserver gave you in the request via the $_SERVER data: $_SERVER('REQUEST_URI')

From that URI, get the path: parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH))

basename() is the right tool to get the last directory once you've distilled a path from the full URI: basename(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH))

function lastPathDir() {
    // get a URI, parse the path from it, get the last directory, & spit it out 
    return basename(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
}

Upvotes: 1

countach
countach

Reputation: 605

This works on windows environtment too, and also works if the path given ends with a slash.

function path_lastdir($p) {
    $p=str_replace('\\','/',trim($p));
    if (substr($p,-1)=='/') $p=substr($p,0,-1);
    $a=explode('/', $p);
    return array_pop($a);
}

Upvotes: 0

Alan Whitelaw
Alan Whitelaw

Reputation: 16900

Explode turns the string into an array, you can then choose the last value in that array to be your result.

$result = end((explode('/', $path)));

Upvotes: 5

Ash501
Ash501

Reputation: 331

I know it's an old question, but this automatically gets the last folder without confusing the last item on the list - that could be the script - and not the actual last folder.

$url = pathinfo($_SERVER['SCRIPT_NAME'], PATHINFO_DIRNAME);
$url_var = explode('/' , $url);
$last_folder = end($url_var);

Upvotes: 4

Matt Lowden
Matt Lowden

Reputation: 2616

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.

Upvotes: 9

Björn Kaiser
Björn Kaiser

Reputation: 9912

<?php
$path = explode('/', $yourPathVar);
// array_pop gives you the last element of an array()
$last = array_pop($path);
?>

Upvotes: 2

Kel
Kel

Reputation: 7790

You can use basename() function:

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

Upvotes: 22

Mark Baker
Mark Baker

Reputation: 212522

$directory = 'Home/new_folder/test';
$path = explode('/',$directory);
$lastDir = array_pop($path);

Upvotes: 2

Related Questions