[Technical house 3] n methods for intercepting files and URL extensions

Source: Internet
Author: User

// N methods for intercepting file extensions

// 1st types

// The strrchr () function is used to locate the last occurrence position of a character in the specified string. If the result is successful, the string following it is returned.

// Return the extension with a bit

Function get_extension ($ file ){

Return strrchr ($ file ,'.');

}

Echo get_extension ("d.sdf.jpeg ");

 

// 2nd types

// The strchr () function is used to locate the first occurrence of a character in a specified string. If the occurrence is successful, the string following the string is returned.

// Use the callback function to remove the points repeatedly.

// Returns the extension without vertices.

Function get_extension ($ file ){

$ Res = trim (strchr ($ file ,'.'),'.');

If (strchr ($ res ,'.')){

$ Res = get_extension ($ res); // use the callback function to remove all vertices repeatedly.

}

Return $ res;

}

Using file='fef.fadsf.fadsf.d.sdf.jpeg ';

Print_r (get_extension ($ file ,'.'));

// Use the WHILE LOOP

// Strstr and strchr are the same function

$ Strstr = "FDS. fdsaf. fdsa/fsda.ees.see.jpg ";

While ($ strstr = strstr ($ strstr ,".")){

$ Strstr = substr ($ strstr, 1 );

If (false === strpos ($ strstr ,".")){

Break;

}

}

Var_dump ($ strstr );

// 3rd types

// Search for the position (case-insensitive) of the last occurrence of a string in another string and return the position number. strrpos is case-sensitive.

// Return the extension with a bit

Function get_extension ($ file ){

$ Begin = strripos ($ file ,'.');

Return substr ($ file, $ begin );

}

Using file='d.sdf.jpeg ';

Echo get_extension ($ file ,'.');

 

// 4th types

// Use explode to split the file name into an array. The last value in the array is the file extension.

// Returns the extension without vertices.

Function get_extension ($ file ){

Return end (explode ('.', $ file ));

// Return array_pop (explode ('.', $ PATH ));

}

Using file='d.sdf.jpeg ';

Echo get_extension ($ file ,'.');

// 5th types

// The pathinfo () function returns the file path information in an array. Array elements: [dirname], [basename], and [extension]

Function get_extension ($ file ){

$ Info = pathinfo ($ file );

Return $ info ['extension'];

// Return pathinfo ($ file, pathinfo_extension );

}

Using file='d.sdf.jpeg ';

Echo get_extension ($ file ,'.');

// 6th types

// Regular Expression matching

// Returns the extension without vertices.

$ Path = "FDS/ees.see.jpg ";

Preg_match ("/. + \. ([^ \.] +)/", $ path, $ array );

Print_r ($ array [1]);

// 7th types

// Regular Expression matching

// Returns the extension without vertices.

$ Path = "FDS/ees.see.jpg ";

Echo (preg_replace ('/. * \. (. * [^ \.]. *) */IU', '\ 1', $ PATH ));

 

// 8th types

// Strtok function: Splits a string into smaller strings.

// Returns the extension without vertices.

$ Path = "FDS/ees.see.jpg ";

$ Strtok = strtok ($ path ,".");

While ($ strtok! = False ){

$ Return = $ strtok;

$ Strtok = strtok ('.');

}

Echo ($ return );

 

// Obtain the URL Extension

// 1st types

// Returns the extension without vertices.

Function getext ($ URL ){

$ Path = parse_url ($ URL );

$ STR = explode ('.', $ path ['path']);

Return $ STR [1];

}

Echo getext ('HTTP: // tools.jb51.net/abc/de/fg.php? Id = 1'); // PHP

// 2nd types

Function retrieve ($ URL ){

Preg_match ('/\/([^ \/] + \. [A-Z] +) [^ \/] * $/', $ URL, $ match); // $ match = array ([0] =>/FG. PHP? Id = 1 [1] => FG. php)

Return strrchr ($ match [1], '.'); // you can use either of the following methods to intercept file extensions: 1, 2, 3, 4, 5, or 8.

}

Print_r (retrieve ('HTTP: // tools.jb51.net/abc/de/fg.php? Id = 1 '); //. php

 

// 3rd types

// Returns the extension without vertices.

Function getext ($ URL ){

$ End = end (explode ('/', $ URL ));

If (stripos ($ end ,'? ') {// When a parameter exists

$ End = substr ($ end, 0, stripos ($ end ,'? '));

}

$ STR = explode ('.', $ end );

Return $ STR [1];

}

 

[Technical house 3] n methods for intercepting files and URL extensions

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.