php One Line Code Obtaining File Suffix Instance Analysis

  • 2021-07-26 07:03:32
  • OfStack

In this paper, an example is given to describe the method of obtaining file suffix name by php1 line code. Share it for your reference. Specific methods are analyzed as follows:

php 1 line of code to get the file suffix method to combine a lot of functions, we are a bit like the function in asp, let's take a look at it.
Example:

$filename  = 'D:/wamp/www/sparkphp/rar';
$rs =  strtolower(trim(substr(strrchr($filename, "."), 1)));

Detailed explanation:
The strrchr () function finds the last occurrence of a string in another string and returns all characters from that position to the end of the string;
The substr () function returns the 1 part of the string, where 1 is read from the first subscript of the string. Until the end;
The trim () function removes the spaces before and after the string;
The strtolower () function converts a string to lowercase.

Supplement other methods:

<?php
 // Method 1:
 function extend_1($file_name)
 {
 $retval="";
 $pt=strrpos($file_name, ".");
 if ($pt) $retval=substr($file_name, $pt+1, strlen($file_name) - $pt);
 return ($retval);
 }
 
 // Method 2
 function extend_2($file_name)
 {
 $extend = pathinfo($file_name);
 $extend = strtolower($extend["extension"]);
 return $extend;
 }
 
 // Method 3
 function extend_3($file_name)
 {
 $extend =explode("." , $file_name);
 $va=count($extend)-1;
 return $extend[$va];
 }
?>

I hope this article is helpful to everyone's PHP programming.


Related articles: