PHP获取文件扩展名(后缀)的6种方法
本文分享PHP获取文件扩展名的6种方法,下面是PHP代码:
那么,到底用哪一种方法呢?
我本人建议使用第6种方法,或者第2种。感觉这两种方法效率应该高一点。
<?php
$filename = 'mypic.gif';
// 1. The "explode/end" approach
$ext = end(explode('.', $filename));
// 2. The "strrchr" approach
$ext = substr(strrchr($filename, '.'), 1);
// 3. The "strrpos" approach
$ext = substr($filename, strrpos($filename, '.') + 1);
// 4. The "preg_replace" approach
$ext = preg_replace('/^.*\.([^.]+)$/D', '$1', $filename);
// 5. The "never use this" approach
// From: http://php.about.com/od/finishedphp1/qt/file_ext_PHP.htm
$exts = split("[/\\.]", $filename);
$n = count($exts)-1;
$ext = $exts[$n];
//6. The "pathinfo" approach
$ext = pathinfo($filename, PATHINFO_EXTENSION);
?>
那么,到底用哪一种方法呢?
我本人建议使用第6种方法,或者第2种。感觉这两种方法效率应该高一点。
浙公网安备 33010602011771号