Well, You get the length of the string: http://php.net/manual/en/function.strlen.php
You take that number and subtract 4 from it. This way you will have your starting value for a substring function: http://be2.php.net/substr
And then you compare that to the string ".jpg" or ".gif" or something like that.
I would put every letter in a for loop and then use strtolower (http://php.net/manual/en/function.strtolower.php) or (http://php.net/manual/en/function.strtoupper.php) to compare. Since caps are not the same as small letters. But maybe there's a better way to do so.