Five methods to get the file extension-reprinted unverified, five file extensions

Source: Internet
Author: User
Tags strtok

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/> ';

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.