Home > php教程 > php手册 > PHP如何获得文件的扩展名

PHP如何获得文件的扩展名

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-13 09:38:08
Original
976 people have browsed it

关于获取文件的后缀名的方法有很多,下面介绍一个吧。

<?php
$file_name = "bkjia.txt";
echo get_exname($file_name);
/**
 * 获取文件扩展名
 * @param unknown_type $file_name
 * @return $ex_name
 */
function get_exname($file_name)
{
	if(empty($file_name)) 
		return false;
    
	$file_name = strtolower($file_name);
    $rev_str = strrev($file_name);
    $ex_name_len = strpos($rev_str,'.');  //扩展名的长度
    $file_name_len = strlen($file_name);
    $ex_name = substr($file_name, $file_name_len - $ex_name_len);
    
    return $ex_name;
}
?>
Copy after login

strtolower() 函数把字符串转换为小写。

strrev() 函数反转字符串。

strpos() 函数返回字符串在另一个字符串中第一次出现的位置。

就是先找到符号'.'的位置,然后计算出扩展名的长度。再用字符串总长度减去扩展名长度来计算出需要截取的长度。

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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template