Home >Backend Development >PHP Problem >What are the several ways to get extensions in php

What are the several ways to get extensions in php

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-09-25 11:17:212861browse

What are the several ways to get extensions in php

php获取扩展名的几种方法,下面给大家一一介绍:

$file = ‘需要进行获取扩展名的文件.php’;

第一种,根据.拆分,获取最后一个元素的值

function getExt1{
return end(explode(".",$file);)
}

第二种,获取最后一个点的位置,截取

function getExt2{
return substr($file,strrpos($file,'.')+1);
}

相关推荐:《php教程

第三种,根据.拆分,获取最后一个元素的值

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;
}

The above is the detailed content of What are the several ways to get extensions in php. 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