This is a written question I encountered when I applied for an internship:
Use more than five ways to get the extension of a file.
Requirements: dir/upload.image.jpg, find. jpg or JPG,
Must be handled using PHP's own processing function, which cannot be significantly duplicated and can be encapsulated into functions such as GET_EXT1 ($file _name), get_ext2 ($file _name)
The following is my reference to the online summary of the five methods, are relatively simple, words not much to say, directly on the code:
Method 1:
function GetExt1 ($filename)
{
$arr = Explode ('. ', $filename);
Return Array_pop ($arr);;
}
Method 2:
function GetExt2 ($filename)
{
$ext = STRRCHR ($filename, '. ');
return $ext;
}
Method 3:
function GetExt3 ($filename)
{
$pos = Strrpos ($filename, '. ');
$ext = substr ($filename, $pos);
return $ext;
}
Method 4:
function GetExt4 ($filename)
{
$arr = PathInfo ($filename);
$ext = $arr [' extension '];
return $ext;
}
Method 5:
function GetExt5 ($filename)
{
$str = Strrev ($filename);
Return Strrev (STRCHR ($str, '. ', true));
}
Five ways PHP Gets the file name extension