PHP,获取没有文件扩展名的文件名

时间:2010-02-02 11:10:29

标签: php file string

我有这个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

我想反过来,我希望函数在没有扩展名的情况下返回my

该文件只是变量中的一个字符串。

17 个答案:

答案 0 :(得分:323)

不需要这一切。查看pathinfo(),它会为您提供路径中的所有组件。

手册中的示例:

$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

代码输出:

/www/htdocs
index.html
html
index

另外,你只能获得某些部分,如:

echo pathinfo('/www/htdocs/index.html', PATHINFO_EXTENSION); // outputs html

答案 1 :(得分:188)

作为pathinfo()的替代方案,您可以使用

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"

你必须事先知道要删除它的扩展名。

但是,由于您的问题表明您需要获取扩展名基本名称,因此我会投票Pekka's answer作为最有用的名称,因为它会为您提供任何信息你想要一个原生函数的路径和文件。

答案 2 :(得分:55)

https://php.net/manual/en/function.pathinfo.php

pathinfo($path, PATHINFO_FILENAME);

简单的功能测试:https://ideone.com/POhIDC

答案 3 :(得分:12)

另一种方法是使用正则表达式。

$fileName = basename($filePath);
$fileNameNoExtension = preg_replace("/\.[^.]+$/", "", $fileName);

这将从上一个句点.中删除,直到字符串结束。

答案 4 :(得分:11)

如果扩展名未知,请使用此解决方案

 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"

PHP MAN function pathinfo

答案 5 :(得分:7)

@Gordon basename如果您知道扩展名将正常工作,如果您不能使用explode:

$filename = end(explode(".", $file));

答案 6 :(得分:7)

几乎所有上述解决方案都显示了从变量$ path

获取文件名

在以下代码段获取当前执行的文件名,不带扩展名

echo pathinfo(basename($_SERVER['SCRIPT_NAME']), PATHINFO_FILENAME);

<强>解释

  

$ _ SERVER ['SCRIPT_NAME']包含当前脚本的路径。

答案 7 :(得分:5)

无需编写大量代码。即使它只能通过一行代码完成。见here

下面是仅返回文件名的一行代码,并删除了扩展名:

<?php
 echo pathinfo('logo.png')['filename'];
?>

会打印

logo

来源:Remove extension and return only file name in PHP

答案 8 :(得分:4)

@ fire如果文件名使用点,你可能得到错误的输出。 我会使用@Gordon方法,但也得到扩展,所以basename函数适用于所有扩展,如下所示:

$path = "/home/httpd/html/index.php";
$ext = pathinfo($path, PATHINFO_EXTENSION);

$file = basename($path, ".".$ext); // $file is set to "index"

答案 9 :(得分:4)

在我的情况下,我在下面使用。我不在乎它的延伸是什么。 :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.

答案 10 :(得分:2)

你可以写这个

$filename = current(explode(".", $file));

如果之前没有使用,它们将返回数组的当前元素。

答案 11 :(得分:2)

<a>

答案 12 :(得分:1)

当您不知道该扩展程序时,文件名没有文件扩展名:

$basename = substr($filename, 0, strrpos($filename, "."));

答案 13 :(得分:1)

如果你不知道你有哪个分机,那么你可以试试这个:

$ext = strtolower(substr('yourFileName.ext', strrpos('yourFileName.ext', '.') + 1));
echo basename('yourFileName.ext','.'.$ext); // output: "youFileName" only

使用所有可能性:

image.jpg // output: "image"
filename.image.png // output: "filename.image"
index.php // output: "index"

答案 14 :(得分:1)

你的答案低于在php中隐藏文件扩展名的完美解决方案。

<?php
    $path = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    echo basename($path, ".php");
?>

答案 15 :(得分:0)

这只返回文件名,在1行中没有任何扩展名:

$path = "/etc/sudoers.php";    
print array_shift(explode(".", basename($path)));
// will print "sudoers"

$file = "file_name.php";    
print array_shift(explode(".", basename($file)));
// will print "file_name"

答案 16 :(得分:0)

当扩展包含多个部分时,现有解决方案将失败。下面的函数可用于多个部分,完整路径或仅文件名:

function removeExt($path)
{
    $basename = basename($path);
    return strpos($basename, '.') === false ? $path : substr($path, 0, - strlen($basename) + strlen(explode('.', $basename)[0]));
}

echo removeExt('https://example.com/file.php');
// https://example.com/file
echo removeExt('https://example.com/file.tar.gz');
// https://example.com/file
echo removeExt('file.tar.gz');
// file
echo removeExt('file');
// file
相关问题