从文件的全名中获取不带扩展名的文件名

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从文件的全名中获取不带扩展名的文件名相关的知识,希望对你有一定的参考价值。

A flexible way to get the file name without the extension
  1. /*
  2. Detailed notes are below the code.
  3. */
  4.  
  5. $str_full_filename = 'tricky_filename_.js.jpg.version2.jpg';
  6. $arr = explode( '.',$str_full_filename );
  7. $int_last_element = sizeof( $arr )-1;// the last element contains the extension
  8. $str_extension = $arr[ $int_last_element ];
  9. array_pop( $arr );// the filename (without the extension), doesn't need the last element
  10. $filename_without_extension = implode( '.',$arr );
  11.  
  12.  
  13. echo 'The extension is '.$str_extension;
  14. echo '<br /> The file name (without the extension) is: '.$filename_without_extension;
  15.  
  16.  
  17. /*
  18. This is a group of methods to get the extension of a file or get a filename without the extension; this is especially useful when you need to copy an image (say filename.jpg), resize and rename to something like the following:
  19.  
  20. 1. filename_small.jpg
  21. 2. filename_medium.jpg
  22. 3. filename_large.jpg
  23.  
  24. I'm not covering how to resize images, just a way to copy the name and the extension so you can keep names consistent.
  25.  
  26. The way I did this is to anticipate strange file names, so files with an extension name in it more than once won't cause errors or inconsistency.
  27. */

以上是关于从文件的全名中获取不带扩展名的文件名的主要内容,如果未能解决你的问题,请参考以下文章

在 Python 中获取不带扩展名的文件名

获取不带扩展名的文件名

从完整路径名中提取文件名(不带扩展名)

Android - 如何获取文件扩展名? [复制]

java - 如何在Java中获取没有扩展名的文件对象的名称? [复制]

Regex-所有不带.html扩展名的PHP文件