How to get the File Extension from a String in PHP Programming 22 JUL 2013

green php elephants - elephpantsThere are plenty of methods of getting the file extension from a file name in PHP, but if you’re looking for a pretty quick and dirty way of achieving this without any hardcore validation, then it becomes a trivial matter of returning the bit of string after the last occurring . (fullstop) in the full file name string.

And given that PHP provides us with the handy strrchr function that finds the last occurrence of a character in a string, we can quickly whip up a file extension returning function that looks no more complex than this:

function get_file_name_extension($filenamestring) {
	return substr(strrchr($filenamestring,'.'),1);


About Craig Lotter

Software developer, husband and dad to two young ladies. Writer behind An Exploring South African. I don't have time for myself any more.