Hi. This should do the trick.
Filename may not have an extension, or it's extension could be virtually any number of characters in length, so don't just grab a pre-determined number of characters from the end of the string.
Explode the filename string into an array of strings, seperated by . and grab last element in array. This will either be the entire filename if no extension existed, or will be filename extension.
This function will return false if supplied $filename is empty.
function GetFileExtension($filename)
{
if (!$filename_parts = explode(".", $filename))
{
return false; // $filename was empty.
}
else
{
return $filename_parts[count($filename_parts) - 1];
}
}
echo GetFileExtension("bitmap.gif"); // Outputs 'gif'
echo GetFileExtension("anotherbitmap.jpeg"); // Outputs 'jpeg'
echo GetFileExtension("flashfile.swf"); // Outputs 'swf'
echo GetFileExtension("entirefilename"); // Outputs 'entirefilename'
Cheers,
Steven