php function to get file extension

怪我咯
Release: 2023-03-12 18:54:02
Original
1106 people have browsed it

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)); 
} 
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!