I will give a lecture to the students today. I will give an example of the array application and explain how to get the suffix of a file. to sum up, there are many methods. I will share them with you below.
Today I will give a lecture to the students. I will give an example of array application and explain how to get the suffix of a file. to sum up, there are many methods. I will share them with you below!
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.
$ Pic = 'abc.3434.342.12123.123.exe ';
$ Pics = explode ('.', $ pic);/* obtain the total number of arrays, and then obtain the last */echo $ num = count ($ pics );
Echo'
'. $ Pics [$ num-1];
/* Traverse the array and obtain the last element */
Foreach ($ pics as $ value) // 2
{
$ A = $ value;
}
Echo $ .'
';/* Directly output the last element of the array */
Echo end ($ pics );
Echo'
';/* The last element of a single-output array. Note the difference with end */
// Echo array_pop ($ pics);/* sort the array in descending order of key values, and then output the first element */
Krsort ($ pics );
Echo array_shift ($ pics );
Echo'
';
/* The value corresponding to the extension index returned by the pathinfo () function */
$ Res = pathinfo ($ pic); // 5
Var_dump ($ res );
Echo $ res ['extension'].'
';
/* 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!