Home > Article > Backend Development > php function to get file extension
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 = 'get_file_ext_' . $i; var_dump($func($file)); } function get_file_ext_1($file) { return strtolower(trim(substr(strrchr($file, '.'), 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, '.')+1))); } function get_file_ext_4($file) { return strtolower(trim(array_pop(explode('.', $file)))); } function get_file_ext_5($file) { $tok = strtok($file, '.'); while($tok !== false) { $return = $tok; $tok = strtok('.'); } 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!