Home  >  Article  >  Backend Development  >  PHP function application using string functions to extract file extensions

PHP function application using string functions to extract file extensions

青灯夜游
青灯夜游Original
2021-08-18 19:06:013597browse

In the previous article "PHP function application using arrays to output file extensions", we introduced two methods of using arrays to obtain file extensions. This time we continue to introduce the method of obtaining the file extension and see how to use the string function. Interested friends can learn about it~

First of all, let’s recall the above. From the above we understand A complete file format can be divided into three parts: the content before the character "." , the character "." , and the content after the character "." .

This time we still have to use the character "." to get the file extension! Below we will introduce to you 3 methods of using string functions to obtain file extensions.

First let’s take a look at the first method of using string functions to obtain file extensions

<?php
function getExtension($filename){
         $ext = strrchr($filename,&#39;.&#39;);
         return $ext;
}
$filename="dir/upload/image.gif";
echo getExtension($filename);
?>

Analyze the key code above:

strrchr($filename,'.') means to return the character "." in the string $filename to the end of the string All characters. Therefore, the output result is:

PHP function application using string functions to extract file extensions

Next let’s look at the second method of using string functions to obtain the file extension

<?php
function getExtension($filename){
    $pos = strrpos($filename, &#39;.&#39;);
    $ext = substr($filename, $pos);
    return $ext;
}
$filename="dir/upload/image.gif";
echo getExtension($filename);
?>

The output result is:

PHP function application using string functions to extract file extensions

##OK, analyze the key code above:

  • First we use

    strrpos($filename, '.')Get the position where the character "." last appeared in the string $filename$pos, and then $ pos is used as the second parameter of the substr() function.

  • substr($filename, $pos)will start intercepting the string based on the position set by $pos$filename, returns all characters starting from $pos to the end of the string. Therefore ".gif" will be output.

If you want to get the extension that does not include the character ".", that is, return "

gif", then you can set substr($filename, $ pos 1), so the output result is:

PHP function application using string functions to extract file extensions

Finally, let’s look at the third method of using string functions to obtain file extensions

<?php
function getExtension($filename){
    $filename = strrev($filename);
    $ext = strstr($str,&#39;.&#39;,true);
    return strrev($ext);
}
$filename="dir/upload/image.gif";
echo getExtension($filename);
?>

Analyze the key code above:


strstr($filename,'.',true)You can get the string $filename All characters from the beginning to the first appearance of to the character ".", that is, get the content before the character ".", and output it to see:

PHP function application using string functions to extract file extensions

This is not in line with our expectations. At this time, I thought about it. Since we are getting the content before the character ".", then reverse the string and let it expand. The characters in the name are not long before the character "."!

So, first use

strrev($filename) to reverse the string $filename, then use strstr($filename,'.',true ) to get the content before the character ".", output this time to see the result:

PHP function application using string functions to extract file extensions

Okay, get the reverse extension, call

strrev($filename)Reverse the string again, so that you can get the normal extension. Look at the output:

PHP function application using string functions to extract file extensions

Okay That’s it for now. If you want to know anything else, you can click here. → →

php video tutorial

Finally, I recommend reading a classic course "

PHP String Processing (Jade Girl Heart Sutra Edition)", it's free~ come and learn !

The above is the detailed content of PHP function application using string functions to extract file extensions. 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