Home  >  Article  >  Backend Development  >  PHP function application uses array to output file extension

PHP function application uses array to output file extension

青灯夜游
青灯夜游Original
2021-08-17 19:13:202613browse

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 or jpg 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 content after the character ".".

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.

<?php
function getExtension($filename)
{
         $arr = explode(&#39;.&#39;,$filename);
         return array_pop($arr);
}
$filename="dir/upload/image.jpg";
echo getExtension($filename);
?>

Output result:

PHP function application uses array to output file extension

OK! Let’s analyze the above code:

First use explode('.',$filename)split the $filename string according to the separator ".", and Pass the substring into an array. Let’s use var_dump($arr) to output this array and take a look:

PHP function application uses array to output file extension

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 function array_pop($arr) to obtain the extension that needs to be obtained from the last element of the $arr array. Output array_pop($arr) and see the result:

echo array_pop($arr);

PHP function application uses array to output file extension

Next let’s look at the second method of getting the file extension .

<?php
function getExtension($filename){
         $arr = pathinfo($filename);
         $ext = $arr[&#39;extension&#39;];
         return $ext;
}
$filename="dir/upload/image.jpg";
echo getExtension($filename);
?>

Output result:

PHP function application uses array to output file extension

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 use var_dump($arr) to output this array and see the result:

PHP function application uses array to output file extension

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn