php 获取文件拓展名,五种方式获取文件扩展名

在PHP面试中,经常碰到此题 :要求写出5种以上的方法,获取一个文件的扩展名,其实也是在考察面试者基础知识的掌握程度,下面整理了几种常用的方法(下面方法返回的都是不带’.'的,如果要求带 ‘.’的话 自己改一下):

$file = ‘siyuantlw/程序设计.php’;

function getExt1($file) {

return substr(strrchr($file,’.'),1);

}

function getExt2($file) {

return substr($file,strrpos($file,’.')+1);

}

function getExt3($file) {

return strrev(substr(strrev($file),0,strpos(strrev($file),’.')));

}

function getExt4($file) {

return array_pop(explode(‘.’,$file)); //array_pop 介绍

}

function getExt5($file) {

$arr = pathinfo($file);

return $arr['extension'];

//或者写成下面这种

//return pathinfo($file,PATHINFO_EXTENSION);

}

function getExt6($file) {

$temp = strtok($file, ‘.’); //strtok函数说明

while($temp !== false ){

$file_extension = $temp;

$temp = strtok(‘.’);

}

return $file_extension;

}

function getExt7($file) {

while($dot = strpos($file, “.”))

{

$file = substr($file, $dot+1);

}

return $file;

}

echo getExt1($file).’
’;

echo getExt2($file).’
’;

echo getExt3($file).’
’;

echo getExt4($file).’
’;

echo getExt5($file).’
’;

echo getExt6($file).’
’;

echo getExt7($file).’
’;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值