php 5種方法獲取副檔名
阿新 • • 發佈:2019-01-28
方法1:使用strrchr()函式
<?php
function getExt($file) {
return strrchr($file, '.');
}
echo getExt('index.php');
?>
注:strrchr() 函式查詢字串在另一個字串中最後一次出現的位置,並返回從該位置到字串結尾的所有字元。如果成失敗,否則返回 false。
方法2:擷取字串
<?php function getExt($file) { return substr($file, strrpos($file, '.')); } echo getExt('index.php'); ?>
注:strrpos() 函式查詢字串在另一個字串中最後一次出現的位置。如果成失敗,否則返回 false。
方法3:使用陣列
<?php
function getExt($file) {
//PHP 5.4開始,會發出警告,因此使用@遮蔽
return @array_pop(explode('.', $file));
}
echo getExt('index.php');
?>
方法4:使用pathinfo()函式
<?php function getExt($file) { $temp = pathinfo($file); return $temp['extension']; } echo getExt('index.php'); ?>
方法五: