Php code
<? php
//method one:
function extend_1 ($ file_name)
{
$ retval = "";
$ pt = strrpos ($ file_name, ".");
if ($ pt) $ retval = substr ($ file_name, $ pt + 1, strlen ($ file_name) - $ pt);
return ($ retval);
}
<? php // Method 1: function extend_1 ($ file_name) {$ retval = ""; $ pt = strrpos ($ file_name, "."); if ($ pt) $ retval = substr ($ file_name, $ pt + 1, strlen ($ file_name) - $ pt); return ($ retval);}
//Method Two
Php code
function extend_2 ($ file_name)
{
$ extend = pathinfo ($ file_name);
$ extend = strtolower ($ extend ["extension"]);
return $ extend;
}
function extend_2 ($ file_name) {$ extend = pathinfo ($ file_name); $ extend = strtolower ($ extend ["extension"]); return $ extend;}
// method three
Php code
function extend_3 ($ file_name)
{
$ extend = explode (".", $ file_name);
$ va = count ($ extend) -1;
return $ extend [$ va];
}
$ extend = explode (".", $ file_name); $ va = count ($ extend) -1; return $ extend [$ va];}
// method four
Php code
function getFileExt ($ file_name)
{
while ($ dot = strpos ($ file_name, "."))
{
$ file_name = substr ($ file_name, $ dot + 1);
}
return $ file_name;
}
?>
function getFileExt ($ file_name) {while ($ dot = strpos ($ file_name, ".")) {$ file_name = substr ($ file_name, $ dot + 1);} return $ file_name;
In addition:
PHP pathinfo () function
PHP Filesystem function definition and usage
The pathinfo () function returns the file path information as an array.
grammar
pathinfo (path, options)
Parameter Description
path
Required. Specify the path to be checked.
process_sections
Optional. Specifies the array element to return. The default is all.
Possible values:
PATHINFO_DIRNAME - return only dirname
PATHINFO_BASENAME - return only basename
PATHINFO_EXTENSION - Only the extension is returned
Description
pathinfo () returns an associative array containing information about the path.
Include the following array elements:
[dirname]
[basename]
[extension]
Tips and Notes NOTE: The pathinfo () function returns a string if it is not required to get all the cells.
Example 1
Php code
<? phpprint_r (pathinfo ("/ testweb / test.txt"));?>
// output:
// Array ([dirname] => / testweb [basename] => test.txt [extension] => txt)
<? phpprint_r (pathinfo ("/ testweb / test.txt")); //> Output: // Array ([dirname] => / testweb [basename] => test.txt [extension] => txt)
Example 2
Php code
<? phpprint_r (pathinfo ("/ testweb / test.txt", PATHINFO_BASENAME));?>
// output:
// test.txt