Home >Backend Development >PHP Tutorial >PHP function application using string functions to extract file extensions
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,'.'); 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:
Next let’s look at the second method of using string functions to obtain the file extension
<?php function getExtension($filename){ $pos = strrpos($filename, '.'); $ext = substr($filename, $pos); return $ext; } $filename="dir/upload/image.gif"; echo getExtension($filename); ?>
The output result is:
##OK, analyze the key code above: 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.
gif", then you can set
substr($filename, $ pos 1), so the output result is:
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,'.',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:
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:
strrev($filename)Reverse the string again, so that you can get the normal extension. Look at the output:
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!