Php dirname, basename, pathinfo parsing directory path

Source: Internet
Author: User

1: string basename (string path [, string suffix]);

A string containing a full path pointing to a file is provided. This function returns the basic file name. If the file name ends with suffix, this part will also be removed.

In Windows, both slash (/) and backslash () can be used as path separators. It is a slash (/) in other environments (/).

Example 1. basename () example



The code is as follows:
<? Php

$ Path = Web page creation tutorial http://www.111cn.net, keep this tag "/home/httpd/html/index. php ";

$ File = basename ($ path); // $ fileissetto "index. php"

$ File = basename ($ path, ". php"); // $ fileissetto "index"

?>


2: string dirname (string path );

The parameter is a file path string and returns the directory after removing the file name.
The path of _ FILE _ is the FILE where the current code is located.
 
Dirname (_ FILE _); the directory name at the top of the FILE is obtained.
Dirname (_ FILE _); the directory name of the FILE's layer is obtained.

Example


The code is as follows:

<? Php
Echo dirname ("c:/testweb/home. php ");
Echo dirname ("/testweb/home. php ");
?> Output:

C:/testweb
/Testweb



The parameter is a file path string. It returns an array containing three parts: directory name, file name, and extension, which are referenced by dirname, basename, and extension.

3: array pathinfo ($ path );

Example 1


The code is as follows:


<? Php
Print_r (pathinfo ("/testweb/test.txt "));
?>

The code is as follows:
Array
(
[Dirname] =>/testweb
[Basename] => test.txt
[Extension] => txt
)



Example 2

Output:


The code is as follows:


<? Php
Print_r (pathinfo ("/testweb/test.txt", PATHINFO_BASENAME ));
?>


Output:

The code is as follows:

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.