Get URL file name suffix

Source: Internet
Author: User
Tags php explode
Use php to analyze the URL to get the file name, directory path, and other data. The principle is to use the PHP explode function to separate strings. To be as efficient as possible, retrieve the file extension from a standard url, and then extend the code to get other data, such as directory path, the principle is to use the explode function of PHP to separate strings.
Example: http://www.abc.com/abc/de/fg.php? Id = 1 need to retrieve php or. php
It is very easy to look at the code directly.

The code is as follows:
$ Url = "http://www.abc.com/abc/de/fg.php? Id = 1 ";

// This is self-written.
Function getUrl ($ url ){
$ Date = explode ('? ', $ Url );
$ Date = basename ($ date [0]);
$ Date = explode ('.', $ date );
Return $ date [1];
}

Var_dump (getUrl ($ url ));

// The following two are obtained online.
Function getExt ($ url ){
$ Arr = parse_url ($ url );

$ File = basename ($ arr ['path']);
$ Ext = explode (".", $ file );
Return $ ext [1];
}

Var_dump (getExt ($ url ));

Function getName ($ url ){

$ W_param = pathinfo ($ url );

$ Str = $ w_param ['extension'];

List ($ type, $ vars) = explode ('? ', $ Str );

Return $ type;

}
Echo 'start3'. date ("Y-m-d H: I: s ");
?>

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.