Home >php教程 >PHP源码 >几种获取路径的后缀名方法

几种获取路径的后缀名方法

PHP中文网
PHP中文网Original
2016-05-25 17:13:231528browse

跳至

$file = "http://www.phpzixue.cn/images/phpzixue.gif"; 

function get_ext1($file)
{ 
return substr(strrchr($file, '.'), 1); //strrchr取得字元最后一次出现处到结尾的字符串
} 
function get_ext2($file)
{ 
return pathinfo($file, PATHINFO_EXTENSION);
//pathinfo以数组的形式返回文件路径的信息。PATHINFO_DIRNAME - 只返回 dirname 
//PATHINFO_BASENAME - 只返回 basename 
//PATHINFO_EXTENSION - 只返回 extension 
} 
function get_ext3($file)
{ 
return substr($file, strrpos($file, '.')+1); //strrpos找出字元最后一次出现的位置
} 
function get_ext4($file)
{ 
return array_pop(explode('.', $file));
//array_pop取得数组的最后一个元素
//explode切开字符串传回一个字符串的数组
} 
function get_ext5($file)
{ 
$tok = strtok($file, '.'); //strtok切开字符串
while($tok !== false)
{ 
   $return = $tok; 
   $tok = strtok('.'); 
} 
return $return; 
} 
echo get_ext1($file);
echo "
";
echo get_ext2($file);
echo "
";
echo get_ext3($file);
echo "
";
echo get_ext4($file);
echo "
";
echo get_ext5($file);
echo "
";
echo (preg_replace('/.*\.(.*[^\.].*)*/iU','\\1',$file));

                   

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn