Alex Rivera | Logout

Extract filename with extension from filepath string

Asked 2010-06-30T14:02:53.563
13

I am looking to get the filename from the end of a filepath string, say

$text = "bob/hello/myfile.zip";

I want to be able to obtain the file name, which I guess would involve getting everything after the last slash as a substring. Can anyone help me with how to do this is PHP? A simple function like:

$fileName = getFileName($text);
Edit
Report

1 Answer

13

For more general needs, use negative value for start parameter.
For e.g.

<?php
$str = '001234567890';
echo substr($str,-10,4);
?>

will output
1234

Using a negative parameter means that it starts from the start'th character from the end.

answered 2013-01-03T06:55:26.483

Your Answer