The first method is completed using arrays. First, cut the file name into an array, and then find a way to get the last element of the array. The second method is to process strings. Another method is to use the pathinfo () function.
Copy codeThe Code is as follows:
$ Pic = 'abc.3434.342.12123.123.exe ';
$ Pics = explode ('.', $ pic );
/* Obtain the total number of arrays and then take the last one */
Echo $ num = count ($ pics );
Echo '<br>'. $ pics [$ num-1];
/* Traverse the array and obtain the last element */
Foreach ($ pics as $ value) // 2
{
$ A = $ value;
}
Echo $ a. '<br> ';
/* Directly output the last element of the array */
Echo end ($ pics );
Echo '<br> ';
/* Pay attention to the difference between the last element of a single-output array and end */
// Echo array_pop ($ pics );
/* Sort the array in reverse order based on the key value, and then output the first element */
Krsort ($ pics );
Echo array_shift ($ pics );
Echo '<br> ';
/* The value corresponding to the extension index returned by the pathinfo () function */
$ Res = pathinfo ($ pic); // 5
Var_dump ($ res );
Echo $ res ['extension']. '<br> ';
/* Extract the string and take the last three digits */
Echo substr ($ pic,-3, 3 );
You can see that there are N kinds of solutions to a problem. This is also the case when you are working on a program. There are always solutions. For some new users, you must stick to them so that you can learn PHP well!