donglie7268 2010-02-02 11:10 采纳率: 100%
浏览 65
已采纳

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

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.

  • 写回答

17条回答 默认 最新

  • duanputian5341 2010-02-02 11:13
    关注

    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'], "
    ";
    echo $path_parts['basename'], "
    ";
    echo $path_parts['extension'], "
    ";
    echo $path_parts['filename'], "
    "; // 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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(16条)

报告相同问题?

悬赏问题

  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi