PHP get file extension Five more ways and comments

Source: Internet
Author: User
Tags strtok

There are a lot of odds of writing five ways to get file extensions in a PHP interview or an exam, and here are some of the ways I've summarized them:

first, the method
$file = ' file to get the Extension. php ';

1. function GetExt1 ($file) {
return substr (strrchr ($file, '. '), 1);
}
2. function GetExt2 ($file) {
return substr ($file, strrpos ($file, '. ') +1);
}
3. Function GETEXT3 ($file) {
return strrev (substr (strrev ($file), 0,strpos (strrev ($file), '. '));
}
4. Function GETEXT4 ($file) {
return array_pop (explode ('. ', $file));
}
5. Function GETEXT5 ($file) {
$arr = Explode ('. ', $file);
$extension = $arr [count ($arr)-1];
Return $extension;
}
6. Function GetExt6 ($file) {
$arr = PathInfo ($file);
return $arr [' extension '];
Or so return pathinfo ($file, pathinfo_extension);
}
7. Function GetExt7 ($file) {
$temp = strtok ($file, '. ');
While ($temp!== False) {
$file _extension = $temp;
$temp = Strtok ('. ');
}
return $file _extension;
}

8. Function GetExt8 ($file) {
While ($dot = Strpos ($file, "."))
{
$file = substr ($file, $dot + 1);
}
Return $file;
}


Comments:
1. STRRCHR (string,char)
Parameters:
String
Necessary. Specifies the string to be Searched.
Char
Necessary. Specifies the character to Find. If the parameter is a number, the character that matches the ASCII value of the number is Searched.
If the argument is more than one character, the first character will Prevail. The function (in Php) looks for the last occurrence of the character in the specified string, starting from the left, and, if successful, returns the character utilises and its subsequent characters, or null if it fails.
2. The STRRCHR () corresponds to the STRCHR (string,char) function, which finds the first occurrence of the specified word utilises and its subsequent characters in the String.
3.strrev ($str) is used to invert strings.
Returns a pointer to a string after the reversed Order.
The 4.array_pop () function deletes the last element in the Array.
Returns the last value of the Array. If the array is empty, or is not an array, NULL is Returned.
The 5.pathinfo (path,options) function returns information about the file path in the form of an array.
Path is Required. Specifies the path to Check.
Options are Optional. Specifies the array element to Return. The default is All.
Optional:
Pathinfo_dirname-return only DIRNAME
Pathinfo_basename-return only BASENAME
Pathinfo_extension-return only EXTENSION
6.
Strtok (str,delim) decomposes A string into a set of strings. S is the character to be decomposed, and Delim is a delimiter character (if the string is passed in, each of the characters in the passed-in string is a delimiter). On first call, s points to the string to be decomposed, and then calls to set S to Null.

PHP get file extension Five more ways and comments

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.