PHP: filename without file extension- best way?

前端 未结 4 923
囚心锁ツ
囚心锁ツ 2020-12-10 06:35

I am trying to pull the filename out of a directory without the extension.

I am kludging my way through with the following:

foreach ($allowed_files a         


        
4条回答
  •  囚心锁ツ
    2020-12-10 07:14

    Try this:

    $noExt = preg_replace("/\\.[^.]*$/", "", $filename);
    

    Edit in response to cletus's comment:
    You could change it in one of a few ways:

    $noExt = preg_replace("/\\.[^.]*$/", "", basename($filename));
    
    // or
    
    $noExt = preg_replace("/\\.[^.\\\\\\/]*$/", "", $filename);
    

    Yes, PHP needs regex literals...

提交回复
热议问题