Mezzan
Mezzan

Reputation: 329

How to retrieve last part of given path?

I'm writing directory listings and want to get the name of the last directory.

Example:

/data/henry/files/gallery

I only want to get the last directory which is gallery.

I tried with substr('abcdef', 1) but it's static.

I have an idea of storing the directories into array and then retrieve the last value but how can I do that?

EDIT:

Thanks @deceze for the easy solution.

My solution was longer and not recommended:

$dirarray = explode("/",$row['source']);
$total = count($dirarray);
$sharename = $dirarray[($total-1)];

Upvotes: 0

Views: 286

Answers (2)

mazter
mazter

Reputation: 136

How about using regular expression? Assuming your text is in $text variable:

preg_match_all("|(/[a-z]+)$|", $text, $result);

$result will contain /gallery.

Upvotes: 0

deceze
deceze

Reputation: 522567

echo basename('/data/henry/files/gallery');

http://php.net/basename

Upvotes: 4

Related Questions