Get file extension [PHP]
Problem
You have a filename and you want to get its file extension, i.e. you want to extract the substring after the last dot.
Solution
/**
* Return the file extension, i.e. take a string and return the
* substring after the last dot.
* If there is no extension, return an empty string.
*
* @return File extension (after the last dot) or empty string (if there is no extension).
*/
static public function get_extension($str)
{
$ext = substr(strrchr($str, '.'), 1);
return $ext;
}
Credits
I found this solution here. I chose this one because it’s simple enough and it returns an empty string if there is no file extension.
Comments (0)
Trackbacks (0)
Leave a comment
Trackback