PHP獲取副檔名
阿新 • • 發佈:2018-11-27
第一種:
<?php
$string= 'dir/upload.image.jpg';
$tok = strtok($string, '.'); //使用strtok將字串分割成一個個令牌
while ($tok)
{
$arr[]= $tok;
$tok = strtok('.'); //該函式會保持它自己的內部指標在字串中的位置,
//如果想重置指標,可以將該字串傳給這個函式.
//所以當第二次呼叫strtok()函式時,如果對上一次的已分割的字串進行分割,第1個引數可以省略
}
$count= count($arr);
$i= $count-1;
$file_type= $arr[$i];
?>
第二種:
<?php
$string= 'dir/upload.image.jpg';
$arr= explode('.', $string); //使用explode()函式分割字串,返回值是一個數組
$count= count($arr);
$count-=1;
$file_type= $arr[$count];//利用數字索引
$file_type = array_pop($arr);//將陣列最後一個單元彈出(出棧),用一個變數接住
?>
第三種:
<?php
$string= 'dir/upload.image.jpg';
$i= strrpos($string, '.'); //得到指定分割符在字串的最後一次出現的位置
$file_type= substr($string, $i);//擷取字串
?>
第四種:
<?php
$string= 'dir/upload.image.jpg';
$file_type= strrchr($string, '.'); //取得某字元最後出現處起的字串。
?>
第五種:
<?php
$string= 'dir/upload.image.jpg';
$arr= pathinfo($string); //返回檔案路徑的資訊 print_r($arr);
$file_type= $arr['extension'];
<?php
$string= 'dir/upload.image.jpg';
$tok = strtok($string, '.'); //使用strtok將字串分割成一個個令牌
while ($tok)
{
$arr[]= $tok;
$tok = strtok('.'); //該函式會保持它自己的內部指標在字串中的位置,
//如果想重置指標,可以將該字串傳給這個函式.
//所以當第二次呼叫strtok()函式時,如果對上一次的已分割的字串進行分割,第1個引數可以省略
}
$count= count($arr);
$i= $count-1;
$file_type= $arr[$i];
?>
第二種:
<?php
$string= 'dir/upload.image.jpg';
$arr= explode('.', $string); //使用explode()函式分割字串,返回值是一個數組
$count= count($arr);
$count-=1;
$file_type= $arr[$count];//利用數字索引
$file_type = array_pop($arr);//將陣列最後一個單元彈出(出棧),用一個變數接住
?>
第三種:
<?php
$string= 'dir/upload.image.jpg';
$i= strrpos($string, '.'); //得到指定分割符在字串的最後一次出現的位置
$file_type= substr($string, $i);//擷取字串
?>
第四種:
<?php
$string= 'dir/upload.image.jpg';
$file_type= strrchr($string, '.'); //取得某字元最後出現處起的字串。
?>
第五種:
<?php
$string= 'dir/upload.image.jpg';
$arr= pathinfo($string); //返回檔案路徑的資訊 print_r($arr);
$file_type= $arr['extension'];
?>
方法1:
function getExt1($filename){ $arr = explode('.',$filename); return array_pop($arr);;}
方法2:
function getExt2($filename){ $ext = strrchr($filename,'.'); return $ext;}
方法3:
function getExt3($filename){ $pos = strrpos($filename, '.'); $ext = substr($filename, $pos); return $ext;}
方法4:
function getExt4($filename){ $arr = pathinfo($filename); $ext = $arr['extension']; return $ext;}
方法5:
function getExt5($filename){ $str = strrev($filename); return strrev(strchr($str,'.',true));}