Home  >  Article  >  Backend Development  >  How to get file suffix (extension) in php

How to get file suffix (extension) in php

WBOY
WBOYOriginal
2016-07-25 09:00:47775browse
  1. //Get the file suffix name

  2. //by bbs.it-home.org

  3. $file_name = "jbxue.com.txt ";

  4. echo get_exname($file_name);
  5. /**
  6. * Get file extension
  7. * @param unknown_type $file_name
  8. * @return $ex_name
  9. */
  10. function get_exname($file_name)
  11. {
  12. if(empty($file_name))
  13. return false;
  14. $file_name = strtolower( $file_name);
  15. $rev_str = strrev($file_name);
  16. $ex_name_len = strpos($rev_str,'.'); //The length of the extension
  17. $file_name_len = strlen($file_name);
  18. $ex_name = substr( $file_name, $file_name_len - $ex_name_len);
  19. return $ex_name;
  20. }
  21. ?>

Copy code

Function explanation: The strtolower() function converts a string to lowercase. The strrev() function reverses a string. The strpos() function returns the position of the first occurrence of a string within another string. That is to first find the position of the symbol '.', and then calculate the length of the extension. Then use the total length of the string minus the length of the extension to calculate the length that needs to be intercepted.

Recall the multiple methods of obtaining file extensions we introduced before: Three ways to get file extension in php (improved version) Summary of methods to obtain php file extension Three ways to get file extension in php 5 ways to get file extension in php A php custom function to get the file extension Several ways to get file extensions in php All roads lead to happiness. I hope you can find the bright sunshine on the road of learning in Programmer's Home and accompany you to spend a good time in your young and colorful life.



Statement:
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