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

方法一:

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__);

 

posted @ 2015-10-16 19:53  lesuso  阅读(239)  评论(0编辑  收藏  举报