PHP,获取没有文件扩展名的文件名 [英] PHP, get file name without file extension

查看:46
本文介绍了PHP,获取没有文件扩展名的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个 PHP 代码:

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]);
    }
}

如果我有一个名为 my.zip 的文件,这个函数返回 .zip.

If I have a file named my.zip, this function returns .zip.

我想做相反的事情,我希望函数返回没有扩展名的my.

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.

推荐答案

不需要所有这些.查看 pathinfo(),它给出你的道路的所有组成部分.

No need for all that. Check out pathinfo(), it gives you all the components of your path.

手册中的示例:

$path_parts = pathinfo('/www/htdocs/index.html');

echo $path_parts['dirname'], "
";
echo $path_parts['basename'], "
";
echo $path_parts['extension'], "
";
echo $path_parts['filename'], "
"; // filename is only since PHP 5.2.0

代码输出:

/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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆