PHP trick code: Get the file extension

Source: Internet
Author: User
Keywords Network programming PHP tutorial
Tags array code dot file function get network network programming

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

Related Article

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.