Summarize the example code for obtaining file extension in PHP

零下一度
Release: 2023-03-10 12:30:01
Original
1313 people have browsed it

这篇文章主要介绍了PHP获取文件扩展名的方法,结合实例形式总结了6种常用的文件扩展名获取方法,代码备有较为详细的注释便于理解,需要的朋友可以参考下

本文实例总结了PHP获取文件扩展名的方法。分享给大家供大家参考,具体如下:

在PHP面试中或者考试中会有很大几率碰到写出五种获取文件扩展名的方法,下面是我自己总结的一些方法


$file = '需要进行获取扩展名的文件.php';
//第一种,根据.拆分,获取最后一个元素的值
function getExt1{
return end(explode(".",$file);)
}
//第二种,获取最后一个点的位置,截取
function getExt2{
return substr($file,strrpos($file,'.')+1);
}
//第三种,根据.拆分,获取最后一个元素的值
function getExt3($file) {
return array_pop(explode('.',$file)); 
}
//第四种,pathinfo 
function getExt5($file) {
$arr = pathinfo($file);
return $arr['extension'];
//或者这样return pathinfo($file,PATHINFO_EXTENSION);
}
//第五种,正则,子模式
function getExt6$file){
preg_match("/(gif | jpg | png)$/",$file,$match);
$match=$match[0];
} 
//第六种,正则反向引用
function getExt7($file){
$match=preg_replace("/.*\.(\w+)/" , "\\1" ,$file );
echo $match;
}
Copy after login

The above is the detailed content of Summarize the example code for obtaining file extension in PHP. 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!