Extract filename with extension from filepath string Extract filename with extension from filepath string php php

Extract filename with extension from filepath string


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.


$text = "bob/hello/myfile.zip";$file_name = end(explode("/", $text));echo $file_name; // myfile.zip

end() returns the last element of a given array.