PHP中獲取文件擴(kuò)展名的方法
第一種:
$file = 'x.y.z.png'; echo substr(strrchr($file, '.'), 1);
解析:strrchr($file, '.')
strrchr() 函數(shù)查找字符串在另一個(gè)字符串中最后一次出現(xiàn)的位置,并返回從該位置到字符串結(jié)尾的所有字符
第二種:
$file = 'x.y.z.png'; echo substr($file, strrpos($file, '.')+1);
解析:strrpos($file, '.')
查找 "." 在字符串中最后一次出現(xiàn)的位置,返回位置 substr()從該位置開始截取
第三種:
$file = 'x.y.z.png'; $arr = explode('.', $file); echo $arr[count($arr)-1];
第四種:
$file = 'x.y.z.png'; $arr = explode('.', $file); echo end($arr); //end()返回?cái)?shù)組的最后一個(gè)元素
第五種:
$file = 'x.y.z.png'; echo strrev(explode('.', strrev($file))[0]);
第六種:
.$file = 'x.y.z.png'; echo pathinfo($file)['extension'];
解析:pathinfo() 函數(shù)以數(shù)組的形式返回文件路徑的信息。
包括以下的數(shù)組元素:
[dirname] [basename] [extension]
第七種:
.$file = 'x.y.z.png'; echo pathinfo($file, PATHINFO_EXTENSION)