search
Homephp教程php手册php中取得文件的后缀名?

php中取得文件的后缀名?

Jun 13, 2016 pm 12:01 PM
phpcuttingusesuffixFinisharraydocumentfile namemethodyesof

第一类方法是利用数组完成的。先把文件名切割成数组,然后想办法取得数组最后一个元素就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学习好!
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools