PHP technical code -- get the file extension

Source: Internet
Author: User

Php code
<? 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 );
}
<? 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 2
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 3
Php code
Function extend_3 ($ file_name)
{
$ Extend = explode (".", $ file_name );
$ Va = count ($ extend)-1;
Return $ extend [$ va];
}
Function extend_3 ($ file_name) {$ extend = explode (".", $ file_name); $ va = count ($ extend)-1; return $ extend [$ va];}
 

// Method 4
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 in an array.
Syntax
Pathinfo (path, options)
Parameters
Description
Path
Required. Specifies the path to be checked.
Process_sections
Optional. Specifies the array element to be returned. The default value is all.
Possible values:
PATHINFO_DIRNAME-returns only dirname
PATHINFO_BASENAME-only return basename
PATHINFO_EXTENSION-returns only extension
Description
Pathinfo () returns an associated array containing path information.
Includes the following array elements:
[Dirname]
[Basename]
[Extension]
Tips and comments
NOTE: If not all units are required, the pathinfo () function returns a string.
Example
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
 

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.