Get the suffix name of the file in php?

  • 2020-05-12 02:25:27
  • OfStack

The first class method is done with arrays. Cut the file name into an array, and then find a way to get the last element of the array to OK. The second method is done through string manipulation, and the other is the pathinfo() function.
 
$pic = 'abc.3434.342.12123.123.exe'; 
$pics = explode('.' , $pic); 

/* I get the total number of arrays, and then I get to the end 1 a */ 
echo $num = count($pics); 
echo '<br>'.$pics[$num-1]; 

/* Go through the groups to get to the end 1 An element */ 
foreach ($pics as $value) //2 
{ 
$a = $value; 
} 
echo $a.'<br>'; 
/* Direct output array at the end 1 An element */ 
echo end($pics); 
echo '<br>'; 
/* Single out the end of the array 1 Elements. Notice the sum end() The difference between */ 
//echo array_pop($pics); 
/* First, arrange the array in reverse order by key, then single out the order 1 An element */ 
krsort($pics); 
echo array_shift($pics); 
echo '<br>'; 

/*pathinfo() The function returns the value extension Index the corresponding value */ 
$res = pathinfo($pic); //5 
var_dump($res); 
echo $res['extension'].'<br>'; 

/* String interception, take the last 3 Who can */ 
echo substr($pic , -3 , 3); 

You can see that there are multiple solutions to a problem with N, the same is true when doing the program, there are always solutions, for some beginners, 1 must adhere to, so as to learn PHP well!

Related articles: