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 by name but not the directory
3.Only taken out The directory path in the path name
4. Or parse each part of the URL to obtain an independent value
5. Or even form a url yourself
... .. ..
You need to use path processing class functions in many places.
We have marked the commonly used path processing functions for everyone. You can just process this path processing function:
Function name | Function |
---|---|
pathinfo | Return the various components of the file |
basename | Return file name |
dirname | File directory part |
parse_url | Unpack the URL into its parts |
http_build_query | Generate the query string in the url |
http_build_url | Generate a url |
##pathinfo
array pathinfo ( string $路径) 功能:传入文件路径返回文件的各个组成部份Let’s use a specific example:
<?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 results are as follows:
文件目录名:d:/www 文件全名:lib.inc.php 文件扩展名:php 不包含扩展的文件名:lib.incbasename
string basename ( string $路径[, string $suffix ]) 功能:传入路径返回文件名 第一个参数传入路径。 第二个参数,指定我文件名到了指定字符停止。
<?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; ?>The execution results are as follows
1: index 2: index.php 3: passwd
dirname
dirname(string $路径) 功能:返回文件路径的文件目录部份
<?php dirname(__FILE__); ?>Conclusion : You can execute it to see if the directory part of the file is returned.
parse_url
mixed parse_url ( string $路径 ) 功能:将网址拆解成各个部份
<?php $url = 'http://username:password@hostname:9090/path?arg=value#anchor'; var_dump(parse_url($url)); ?>The results are as follows:
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_query
string http_build_query ( mixed $需要处理的数据) 功能:生成url 中的query字符串
<?php //定义一个关联数组 $data = [ 'username'=>'php', 'area'=>'hubei' ]; //生成query内容 echo http_build_query($data); ?>The results are as follows:
username=php&area=hubei
http_build_url() Function: Generate a url
PHP_EOL constant
Equivalent to echo "\r\n" on the windows platform;
Equivalent to echo "\n" on the unix\linux platform;
Equivalent to echo "\r" on the mac platform;
Next Section