Php get the file name suffix instance summary

Source: Internet
Author: User
Tags extend php code

1. basename ()-returned path file name

See the following php code:

The code is as follows: Copy code

<? Php
$ Path = "/usr/www/html/index. php ";
Echo basename ($ path). "<br> ";
// If suffix is selected, the extension is ignored.
Echo basename ($ path, ". php ");
?>


Running result:
Index. php
Index
2. dirname ()-returns the file path of the current script!
Php code:

The code is as follows: Copy code


<? Php
// -- FILE _ returns the complete FILE path
$ Dir = dirname (_ FILE __);
Echo $ dir;
?>

Running result:
F: webzendexercise

3. pathinfo () returns an associated array containing path information.
It includes the following array units: path name dirname, file name basename, and extension name extension.
See the following simple code demonstration:

 

The code is as follows: Copy code
<? Php
$ Path = "/usr/www/html/index. php ";
$ Pathinfo = pathinfo ($ path );
Echo "directory name: $ pathinfo [dirname] <br> ";
Echo "File name: $ pathinfo [basename] <br> ";
Echo "extension: $ pathinfo [extension]";
?>

Running result:
Directory name:/usr/www/html
File name: index. php
Extension: php
4. realpath -- returns the normalized absolute path name.
The php code is as follows:

The code is as follows: Copy code


<? Php
$ Path = "./exercise/php.txt ";
$ Realpath = realpath ($ path );
Echo $ realpath;
?>

Note one tips: the file path operators in different paths may be different. In windows, you can use "/" and "",
Only "/" can be used in linux, so we recommend that you use "/" during development, such as the file path above!

<? Php
// Method 1:

The code is as follows: Copy code
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

The code is as follows: Copy code
Function extend_2 ($ file_name)
{
$ Extend = pathinfo ($ file_name );
$ Extend = strtolower ($ extend ["extension"]);
Return $ extend;
}


// Method 3
Php code

The code is as follows: Copy code
Function extend_3 ($ file_name)
{
$ Extend = explode (".", $ file_name );
$ Va = count ($ extend)-1;
Return $ extend [$ va];
}


// Method 4
Php code

The code is as follows: Copy code
Function getFileExt ($ file_name)
{
While ($ dot = strpos ($ file_name, ".")
{
$ File_name = substr ($ file_name, $ dot + 1 );
}
Return $ file_name;
}

?>

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.