使用五种以上方式获取一个文件的扩展名 2016.6.1

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

<?php
 
 $file = ‘siyuantlw/程序设计.php’;
 
 function getExt1($file) {
  returnsubstr(strrchr($file,’.'),1);
 }
 function getExt2($file) {
  returnsubstr($file,strrpos($file,’.')+1);
 }
 function getExt3($file) {
  returnstrrev(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'];
  //或者写成下面这种
  //returnpathinfo($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).’<br/>’;
 echo getExt2($file).’<br/>’;
 echo getExt3($file).’<br/>’;
 echo getExt4($file).’<br/>’;
 echo getExt5($file).’<br/>’;
 echo getExt6($file).’<br/>’;

echo getExt7($file).’<br />’;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值