In the previous article, we introduced the method of using PHP to calculate the total number of days for a given year, month and day. If you are interested, you can click on the link to read → "How to calculate the total number of days for a given year, month and day through PHP》. This time we will introduce how to use PHP to get the extension of the file. Friends in need can learn about it~
We have such a file URL:
dir/upload/image.jpg
How to get the extension of the file.jpg
orjpg
What about the output? Implementation idea:
The format of a complete file is:File name.Extension
. Therefore, we can divide the file URL into three parts according to the characters ".
":The content before the character ".",Characters ".",The content after the character ".". We only need to get the contentafter thecharacter ".".
Let’s introduce two methods of using arrays to obtain file extensions.
First let’s take a look at the first method of getting the file extension.
Output result:
OK! Let’s analyze the above code:
First useexplode('.',$filename)
split the$filename
string according to the separator ".", and Pass the substring into an array. Let’s usevar_dump($arr)
to output this array and take a look:
It can be seen that there are two elements, and the array element at the end is what needs to be obtained. extension name.
In this way, we directly use the built-in functionarray_pop($arr)
to obtain the extension that needs to be obtained from the last element of the$arr
array. Outputarray_pop($arr)
and see the result:
echo array_pop($arr);
Next let’s look at the second method of getting the file extension .
Output result:
OK! You can also get the extension. Let’s analyze the above code:
pathinfo($filename)
You can return information about the file path in the form of an array. The array elements returned by this function are:
[dirname]
:Directory path
[basename]
:File name
[extension]
: File suffix name
[filename]
: File without suffix Name
We usevar_dump($arr)
to output this array and see the result:
Okay It can be seen that the key value of the array element with the key name "extension
" is the file extension "jpg
" we need to obtain. In this way, we can directly use$arr['extension']
to access the array element with the key name "extension" and obtain the file extension.
Okay, that’s all. If you want to know anything else, you can click this. → →php video tutorial
Finally, I would like to recommend a free video tutorial on PHP arrays:PHP function array array function video explanation, come and learn!
The above is the detailed content of PHP function application uses array to output file extension. For more information, please follow other related articles on the PHP Chinese website!