Home > Backend Development > PHP Tutorial > Summary of methods to obtain php file extension

Summary of methods to obtain php file extension

WBOY
Release: 2016-07-25 09:00:46
Original
1412 people have browsed it
  1. //取文件的扩展名

  2. //by http://bbs.it-home.org
  3. $file = "/home/jbxue/file_20130322.txt";

  4. for($i=1; $i < 6; $i++) {

  5. $func = 'get_file_ext_' . $i;
  6. var_dump($func($file));
  7. }

  8. function get_file_ext_1($file) {

  9. return strtolower(trim(substr(strrchr($file, '.'), 1)));
  10. }

  11. function get_file_ext_2($file) {

  12. return strtolower(trim(pathinfo($file, PATHINFO_EXTENSION)));
  13. }

  14. function get_file_ext_3($file) {

  15. return strtolower(trim(substr($file, strrpos($file, '.')+1)));
  16. }

  17. function get_file_ext_4($file) {

  18. return strtolower(trim(array_pop(explode('.', $file))));
  19. }

  20. function get_file_ext_5($file) {

  21. $tok = strtok($file, '.');
  22. while($tok !== false) {
  23. $return = $tok;
  24. $tok = strtok('.');
  25. }
  26. return strtolower(trim($return));
  27. }
  28. ?>

复制代码

附:php 文件扩展名小知识 文件扩展名是操作系统用来标志文件格式的一种机制。 通常来说,一个扩展名是跟在主文件名后面的,由一个分隔符分隔。 在一个像“readme.txt”的文件名中,readme是主文件名,txt为扩展名,表示这个文件被认为是一个纯文本文件。



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template