Home > php教程 > php手册 > body text

php中取得文件的后缀名?

WBOY
Release: 2016-06-13 12:01:39
Original
928 people have browsed it

第一类方法是利用数组完成的。先把文件名切割成数组,然后想办法取得数组最后一个元素就OK了。第二类方法是通过字符串的处理来完成,还有一种方法是用pathinfo()函数。

复制代码 代码如下:


$pic = 'abc.3434.342.12123.123.exe';
$pics = explode('.' , $pic);

/*得到数组总数,然后取最后一个*/
echo $num = count($pics);
echo '
'.$pics[$num-1];

/*遍历数组,取得最后一个元素*/
foreach ($pics as $value) //2
{
$a = $value;
}
echo $a.'
';
/*直接输出数组最后一个元素*/
echo end($pics);
echo '
';
/*单出数组最后一个元素,注意和end()的区别*/
//echo array_pop($pics);
/*先按照键值倒序排列这个数组,然后单出第一个元素*/
krsort($pics);
echo array_shift($pics);
echo '
';

/*pathinfo()函数返回值的extension索引对应的值*/
$res = pathinfo($pic); //5
var_dump($res);
echo $res['extension'].'
';

/*字符串截取,取最后三位即可*/
echo substr($pic , -3 , 3);


大家可以看到一个问题有N多种解决办法,在做程序的时候同样如此,解决方法总是有的,对于一些新手,一定要坚持,这样才能把PHP学习好!
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template