首页 > 其他 > 详细

使用五种方法获取文件扩展名

时间:2015-10-16 20:28:24      阅读:297      评论:0      收藏:0      [点我收藏+]

方法一:

function get_ext1($path)
{
    return strrchr($path,‘.‘);
} 

echo get_ext1(__FILE__);

方法二:

function get_ext2($path)
{
    return substr($path,strrpos($path, ‘.‘));
} 

echo get_ext2(__FILE__);

方法三:

function get_ext3($path)
{
    $result = pathinfo($path);
    //array(4) { ["dirname"]=> string(26) "D:\wamp\apache\htdocs\test" ["basename"]=> string(10) "demo29.php" ["extension"]=> string(3) "php" ["filename"]=> string(6) "demo29" } 
    return $result[‘extension‘];
} 

var_dump(get_ext3(__FILE__));

方法四:

function get_ext4($path)
{
    $result = explode(‘.‘, $path);
    return $result[count($result)-1];
} 

echo get_ext4(__FILE__);

方法五:

function get_ext5($path)
{
    $pattern = ‘/^[^\.]+\.([\w]+)$/‘;
    return preg_replace($pattern,‘${1}‘, basename($path));
} 

echo get_ext5(__FILE__);

 

使用五种方法获取文件扩展名

原文:http://www.cnblogs.com/lesuso/p/4886230.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!