php 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 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 ]) 功能:傳入路徑返回文件名 第一個參數(shù)傳入路徑。 第二個參數(shù),指定我文件名到了指定字符停止。
<?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 $需要處理的數(shù)據) 功能:生成url 中的query字符串
<?php //定義一個關聯(lián)數(shù)組 $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;