Five methods to get the file extension-reprinted unverified, five file extensions
In PHP interviews, I often encounter this question: I need to write more than five methods to obtain the extension of a file. In fact, I am also examining the basic knowledge of the interviewer, the following describes several common methods (the methods returned below do not contain '. ', if required '. ):
<? Php
$ File = 'siyuantlw/Program Design. php ';
Function getExt1 ($ file ){
Return substr (strrchr ($ file, '.'), 1 );
}
Function getExt2 ($ file ){
Return substr ($ file, strrpos ($ file, '.') + 1 );
}
Function getExt3 ($ file ){
Return strrev (substr (strrev ($ file), 0, strpos (strrev ($ file ),'.')));
}
Function getExt4 ($ file ){
Return array_pop (explode ('.', $ file); // array_pop Introduction
}
Function getExt5 ($ file ){
$ Arr = pathinfo ($ file );
Return $ arr ['extension'];
// Or write the following
// Return pathinfo ($ file, PATHINFO_EXTENSION );
}
Function getExt6 ($ file ){
$ Temp = strtok ($ file, '.'); // strtok Function Description
While ($ temp! = False ){
$ File_extension = $ temp;
$ Temp = strtok ('.');
}
Return $ file_extension;
}
Function getExt7 ($ file ){
While ($ dot = strpos ($ file, ".")
{
$ File = substr ($ file, $ dot + 1 );
}
Return $ file;
}
Echo getExt1 ($ file). '<br/> ';
Echo getExt2 ($ file). '<br/> ';
Echo getExt3 ($ file). '<br/> ';
Echo getExt4 ($ file). '<br/> ';
Echo getExt5 ($ file). '<br/> ';
Echo getExt6 ($ file). '<br/> ';
Echo getExt7 ($ file). '<br/> ';