PHP,获取文件名,无需扩展名 [英] PHP, get file name without file extension
本文介绍了PHP,获取文件名,无需扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
function ShowFileExtension($ filepath)
{
preg_match '/ [^?] * /',$ filepath,$ matches);
$ string = $ matches [0];
$ pattern = preg_split('/ \。/',$ string,-1,PREG_SPLIT_OFFSET_CAPTURE);
if(count($ pattern)> 1)
{
$ filenamepart = $ pattern [count($ pattern)-1] [0];
preg_match('/ [^?] * /',$ filenamepart,$ matches);
return strtolower($ matches [0]);
}
}
如果我有一个名为 my.zip
,此函数返回 .zip
。
我想做相反,我希望函数返回我的
而不用扩展名。
该文件只是一个字符串
解决方案
不需要这一切。查看 pathinfo() ,它给出您的路径的所有组件。
手册示例:
$ path_parts = pathinfo('/ www / htdocs / index.html');
echo $ path_parts ['dirname'],\\\
;
echo $ path_parts ['basename'],\\\
;
echo $ path_parts ['extension'],\\\
;
echo $ path_parts ['filename'],\\\
; // filename只有从PHP 5.2.0
代码的输出:
/ www / htdocs
index.html
html
索引
或者,您只能获得以下特定部分:
echo pathinfo('/ www / htdocs / index.html',PATHINFO_EXTENSION); //输出html
I have this PHP code:
function ShowFileExtension($filepath)
{
preg_match('/[^?]*/', $filepath, $matches);
$string = $matches[0];
$pattern = preg_split('/\./', $string, -1, PREG_SPLIT_OFFSET_CAPTURE);
if(count($pattern) > 1)
{
$filenamepart = $pattern[count($pattern)-1][0];
preg_match('/[^?]*/', $filenamepart, $matches);
return strtolower($matches[0]);
}
}
If I have a file named my.zip
, this function returns .zip
.
I want to do the reverse, I want the function to return my
without the extension.
The file is just a string in a variable.
解决方案
No need for all that. Check out pathinfo(), it gives you all the components of your path.
Example from the manual:
$path_parts = pathinfo('/www/htdocs/index.html');
echo $path_parts['dirname'], "\n";
echo $path_parts['basename'], "\n";
echo $path_parts['extension'], "\n";
echo $path_parts['filename'], "\n"; // filename is only since PHP 5.2.0
Output of the code:
/www/htdocs
index.html
html
index
And alternatively you can get only certain parts like:
echo pathinfo('/www/htdocs/index.html', PATHINFO_EXTENSION); // outputs html
这篇关于PHP,获取文件名,无需扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文