File path function
We often encounter the situation of processing file paths.
For example:
1. The file suffix needs to be taken out
2. The path needs to be taken out from the name but not the directory
3. Only the path name needs to be taken out Directory path
4. Or parse each part of the URL to obtain independent values
5. Or even form a URL yourself
... ....
Many places require the use of path processing class functions.
We have marked the commonly used path processing functions for everyone. You can just process this path processing function:
pathinfo
array pathinfo (string $path)
Function: Pass in the file path and return the various components of the file
We use specific examples to Use it:
<?php $path_parts = pathinfo('d:/www/index.inc.php'); echo '文件目录名:'.$path_parts['dirname']."<br />"; echo '文件全名:'.$path_parts['basename']."<br />"; echo '文件扩展名:'.$path_parts['extension']."<br />"; echo '不包含扩展的文件名:'.$path_parts['filename']."<br />"; ?>
The result is as follows:
File directory name: d:/www
Full file name: lib.inc.php
File extension: php
File name without extension: lib.inc
##basename##string basename (string $path[, string $suffix])
Function: Pass in the path and return the file name Pass in the path as the first parameter.
The second parameter specifies that my file name will stop when it reaches the specified character.
<?php echo "1: ".basename("d:/www/index.d", ".d").PHP_EOL; echo "2: ".basename("d:/www/index.php").PHP_EOL; echo "3: ".basename("d:/www/passwd").PHP_EOL; ?>
1: index
2: index.php 3: passwd
dirnamedirname(string $path)
Function: Return the file directory part of the file path<?php dirname(__FILE__); ?>
parse_urlmixed parse_url (string $path)
Function: Split the URL into various parts<?php $url = 'http://username:password@hostname:9090/path?arg=value#anchor'; var_dump(parse_url($url)); ?>
array(8) {
["scheme"]=> string(4) "http" ["host"]=> string(8) " hostname"
["port"]=> int(9090)
["user"]=> string(8) "username"
["pass"]=> string(8) "password"
["path"]=> string(5) "/path"
["query"]=> string(9) "arg=value"
["fragment"] => string(6) "anchor"
}
http_build_querystring http_build_query (mixed $data to be processed)
Function: Generate the query string in the url<?php //定义一个关联数组 $data = [ 'username'=>'liwenkai', 'area'=>'hubei' ]; //生成query内容 echo http_build_query($data); ?>
username=liwenkai&area=hubei
http_build_url()
Function: Generate a url Note:
PHP_EOL constant
Equivalent to echo "\r\n" on the windows platform;
Equivalent to echo "\n" on the unix\linux platform;
Equivalent to echo "\n" on the mac platform echo "\r";