Home  >  Article  >  Backend Development  >  Simple function to get file extension

Simple function to get file extension

WBOY
WBOYOriginal
2016-07-25 09:02:31915browse
The method is a bit clumsy, but the idea is relatively clear, and it is written for novices, so the explanation is a bit long-winded, so experts should avoid it :)
Reprinted from PHP interview questions: http://phpmst.com/

  1. $path=str_replace('\','/',__FILE__);//Slightly adjust the format of the file path and replace \ with /
  2. function substr_1($path){
  3. $ str_1= strrchr($path,'.');//Get the content after . and . in the file path
  4. $str_2=(strpos($str_1,'?')===false)?$str_1:preg_replace(' /[?][w]*/','',$str_1);
  5. /*
  6. Determine whether the file contains parameters. If you just get the file on the computer, there will definitely be no parameters. You can ignore this step. , but if it is a url, it may have parameters such as ?a=444&b=33. We only need to get the extension name, so we need to remove these parameters. Here we use regular expressions to replace them all with empty characters. ;
  7. */
  8. return ltrim($str_2,'.');//If you want to get the ".php" format, you can remove this step. If you want to get the "php" format, keep this step;
  9. }
  10. //The following is the test
  11. echo substr_1($path);
  12. ?>
Copy code


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