PHP获取文件扩展名相关收集

多种方法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;
}

 

一款实用php 正则文件扩展名代码

*/ 
function attachicon($type,$size='') { 
 static $attachicons = array( 
 0 => 'common.gif', 
 1 => 'image.gif', 
 2 => 'binary.gif', 
 3 => 'rar.gif', 
 4 => 'msoffice.gif', 
 5 => 'text.gif', 
 6 => 'html.gif', 
 7 => 'real.gif', 
 8 => 'av.gif', 
 9 => 'flash.gif', 
 10 => 'pdf.gif', 
 11 => 'torrent.gif' 
 ); 
 
 if(preg_match("/image|^(jpg|gif|png|bmp)/", $type)) { 
  $typeid = 1; 
 } elseif(preg_match("/bittorrent|^torrent/", $type)) { 
  $typeid = 11; 
 } elseif(preg_match("/pdf|^pdf/", $type)) { 
  $typeid = 10; 
 } elseif(preg_match("/flash|^(swf|fla|swi)/", $type)) { 
  $typeid = 9; 
 } elseif(preg_match("/audio|video|^(wav|mid|mp3|m3u|wma|asf|asx|vqf|mpg|mpeg|avi|wmv)/", $type)) { 
  $typeid = 8; 
 } elseif(preg_match("/real|^(rm|rv)/", $type)) { 
  $typeid = 7; 
 } elseif(preg_match("/htm|^(php|js|pl|cgi|asp教程)/", $type)) { 
  $typeid = 6; 
 } elseif(preg_match("/text|^(txt|rtf|wri|chm)/", $type)) { 
  $typeid = 5; 
 } elseif(preg_match("/word|powerpoint|^(doc|ppt)/", $type)) { 
  $typeid = 4; 
 } elseif(preg_match("/compressed|^(zip|arj|rar|arc|cab|lzh|lha|tar|gz)/", $type)) { 
  $typeid = 3; 
 } elseif(preg_match("/octet-stream|^(exe|com|bat|dll)/", $type)) { 
  $typeid = 2; 
 } else { 
  $typeid = 0; 
 } 
 return $size.$attachicons[$typeid]; 
} 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乡村

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值