PHP,获取没有文件扩展名的文件名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2183486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP, get file name without file extension
提问by Mostafa Elkady
I have this PHP code:
我有这个 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]);
}
}
If I have a file named my.zip, this function returns .zip.
如果我有一个名为 的文件my.zip,则此函数返回.zip.
I want to do the reverse, I want the function to return mywithout the extension.
我想做相反的事情,我希望函数在my没有扩展名的情况下返回。
The file is just a string in a variable.
该文件只是变量中的一个字符串。
回答by
No need for all that. Check out pathinfo(), it gives you all the components of your path.
不需要这一切。查看pathinfo(),它为您提供路径的所有组件。
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
回答by Gordon
As an alternative to pathinfo(), you can use
作为替代pathinfo(),您可以使用
basename()— Returns filename component of path
basename()— 返回路径的文件名部分
Example from PHP manual
PHP 手册中的示例
$path = "/home/httpd/html/index.php";
$file = basename($path); // $file is set to "index.php"
$file = basename($path, ".php"); // $file is set to "index"
You have to know the extension to remove it in advance though.
不过,您必须知道扩展名才能提前将其删除。
However, since your question suggests you have the need for getting the extension andthe basename, I'd vote Pekka's answeras the most useful one, because it will give you any info you'd want about the path and file with one single native function.
但是,由于您的问题表明您需要获取扩展名和基本名称,因此我将Pekka 的答案选为最有用的答案,因为它会为您提供有关路径和文件的任何信息功能。
回答by yckart
https://php.net/manual/en/function.pathinfo.php
https://php.net/manual/en/function.pathinfo.php
pathinfo($path, PATHINFO_FILENAME);
Simple functional test: https://ideone.com/POhIDC
简单的功能测试:https: //ideone.com/POhIDC
回答by caiosm1005
Another approach is by using regular expressions.
另一种方法是使用正则表达式。
$fileName = basename($filePath);
$fileNameNoExtension = preg_replace("/\.[^.]+$/", "", $fileName);
This removes from the last period .up until the end of the string.
这将从最后一个句点删除,.直到字符串的末尾。
回答by anydasa
If the extension is not known, use this solution
如果扩展名未知,请使用此解决方案
pathinfo('D:/dir1/dir2/fname', PATHINFO_FILENAME); // return "fname"
pathinfo('D:/dir1/dir2/fname.php', PATHINFO_FILENAME); // return "fname"
pathinfo('D:/dir1/dir2/fname.jpg', PATHINFO_FILENAME); // return "fname"
pathinfo('D:/dir1/dir2/fname.jpg', PATHINFO_DIRNAME) . '/' . pathinfo('D:/dir1/dir2/fname.jpg', PATHINFO_FILENAME); // return "D:/dir1/dir2/fname"
回答by Rohan Khude
Almost all the above solution have the shown getting filename from variable $path
几乎所有上述解决方案都显示了从变量 $path 获取文件名
Below snippet will get the current executed file name without extension
下面的代码段将获取当前执行的文件名,没有扩展名
echo pathinfo(basename($_SERVER['SCRIPT_NAME']), PATHINFO_FILENAME);
Explanation
解释
$_SERVER['SCRIPT_NAME'] contains the path of the current script.
$_SERVER['SCRIPT_NAME'] 包含当前脚本的路径。
回答by Faruque Ahamed Mollick
There is no need to write lots of code. Even it can be done just by one line of code. See here
无需编写大量代码。甚至可以通过一行代码来完成。看这里
Below is the one line code that returns the filename only and removes extension name:
以下是仅返回文件名并删除扩展名的一行代码:
<?php
echo pathinfo('logo.png')['filename'];
?>
It will print
它会打印
logo
回答by fire
@Gordon basename will work fine if you know the extension, if you dont you can use explode:
如果您知道扩展名,@Gordon basename 可以正常工作,如果您不知道,则可以使用爆炸:
$filename = end(explode(".", $file));
回答by NSINE
@fire incase the filename uses dots, you could get the wrong output. I would use @Gordon method but get the extension too, so the basename function works with all extensions, like this:
@fire 以防文件名使用点,您可能会得到错误的输出。我会使用@Gordon 方法,但也获取扩展名,因此 basename 函数适用于所有扩展名,如下所示:
$path = "/home/httpd/html/index.php";
$ext = pathinfo($path, PATHINFO_EXTENSION);
$file = basename($path, ".".$ext); // $file is set to "index"
回答by Hoàng V? Tgtt
in my case, i use below. I don't care what is its extention. :D i think it will help you
就我而言,我在下面使用。我不在乎它的范围是什么。:D 我认为它会帮助你
$exploded_filepath = explode(".", $filepath_or_URL);
$extension = end($exploded_filepath);
echo basename($filepath_or_URL, ".".$extension ); //will print out the the name without extension.

