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/
- $path=str_replace('\','/',__FILE__);//Slightly adjust the format of the file path and replace \ with /
- function substr_1($path){
- $ str_1= strrchr($path,'.');//Get the content after . and . in the file path
- $str_2=(strpos($str_1,'?')===false)?$str_1:preg_replace(' /[?][w]*/','',$str_1);
- /*
- 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. ;
- */
- 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;
- }
- //The following is the test
- echo substr_1($path);
- ?>
Copy code
|