Home  >  Article  >  Backend Development  >  php function to get file extension

php function to get file extension

怪我咯
怪我咯Original
2017-07-05 10:47:501146browse

Sometimes we need to get the file extension, classify files, etc. The following is the PHP function example code.

The code is as follows:

<?php 
$file = "/home/lvyaozu/backup_20080115.txt"; 

for($i=1; $i < 6; $i++) { 
$func = &#39;get_file_ext_&#39; . $i; 
var_dump($func($file)); 
} 


function get_file_ext_1($file) { 
return strtolower(trim(substr(strrchr($file, &#39;.&#39;), 1))); 
} 

function get_file_ext_2($file) { 
return strtolower(trim(pathinfo($file, PATHINFO_EXTENSION))); 
} 

function get_file_ext_3($file) { 
return strtolower(trim(substr($file, strrpos($file, &#39;.&#39;)+1))); 
} 

function get_file_ext_4($file) { 
return strtolower(trim(array_pop(explode(&#39;.&#39;, $file)))); 
} 

function get_file_ext_5($file) { 
$tok = strtok($file, &#39;.&#39;); 
while($tok !== false) { 
$return = $tok; 
$tok = strtok(&#39;.&#39;); 
} 
return strtolower(trim($return)); 
} 
?>

The above is the detailed content of php function to get 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