How to get the file suffix?

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.