SPLFileInfo: get filename without extension

ⅰ亾dé卋堺 提交于 2019-12-03 11:36:00

I'm late and all the credits should go to @salathe being the first helping the OP, but here's the link to PHP manual for basename function. Basically, you get the extension, then prepend a dot . and finally use the basename function to get the name, like this:

$file->getBasename('.' . $file->getExtension())

For fast reading, here's the PHP manual page's example:

$info = new SplFileInfo('file.txt');
var_dump($info->getBasename());

$info = new SplFileInfo('/path/to/file.txt');
var_dump($info->getBasename());

$info = new SplFileInfo('/path/to/file.txt');
var_dump($info->getBasename('.txt'));

Output:

string(8) "file.txt"
string(8) "file.txt"
string(4) "file" 

I couldnt find anything official like the .net's - Path.GetFileNameWithoutExtension https://msdn.microsoft.com/en-us/library/system.io.path.getfilenamewithoutextension%28v=vs.110%29.aspx

Here is what I'm using instead preg_replace("/.[^.]+$/", "", $file->getBasename());

I've tested it with "file.txt.zip" to make sure it returns "file.txt", not "file"

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!