获取文件后缀的几种方法

<?php
/** 
燕十八 公益PHP培训 
课堂地址:YY频道88354001 
学习社区:www.zixue.it 
**/

$file = 'eg.jpeg.exe';

// 方法1直接获取最后以.的后面的字符串,就拿到了后缀
function getval($file) {
    return strrchr($file,'.');
}

echo getval($file),'<br />';

echo "<hr/>";//动人的分割线
//方法 2 先拿到小.的最后出现的位置,然后出现的位置向后截取拿到后缀
function getval2($file) {
    return substr($file,strrpos($file,'.'));
}
echo getval2($file),'<br />';

echo '<hr />';// 迷人的分割线;

//方法3 先将字符串倒转 strrev函数实现,然后拿到第一个小.出现的位置从0位置开始截取拿到后缀.
function getval3($file) {
    $file = strrev($file);
    return strrev(substr($file,0,strpos($file,'.')));
}

echo getval3($file),'<br />';

echo '<hr/>';//销魂的分割线;
//方法4 php内置函数,简单易用.直接拿到没压力
function getval4($file) {
    /*
    $arr = pathinfo($file);
    return $arr['extension'];
    */

    return pathinfo($file,PATHINFO_EXTENSION);

}

echo getval4($file);

echo '<hr/>'; //幽默的分割线;
//方法5 先将字串倒转,然分割成数组直接去处第一个就可以了
function getval5($file){
        
        $res=str_split(strrev($file),'.');
       return $res[0];
    
}
     echo getval5($file);

    echo '<hr/>';//无趣的分割线

 

posted @ 2012-10-04 12:22  arvim  阅读(721)  评论(0编辑  收藏  举报