php file path handler

  • Filename component of the return path - basename
# 语法
string basename ( string $path [, string $suffix ] )
//> 手册例子
echo "1) ".basename("/etc/sudoers.d", ".d").PHP_EOL; //> sudoers echo "2) ".basename("/etc/passwd").PHP_EOL; //> passwd echo "3) ".basename("/etc/").PHP_EOL; //> etc echo "4) ".basename(".").PHP_EOL; //> . echo "5) ".basename("/"); //> # 参数 $suffix 去除指定部分 # PHP_EOL php常量 换行符 windows 相当于"\r\n" | linux/nuix 相当于"\n" | mac 相当于"\r" 
  • dirname - Returns the directory path portion
# 语法
string dirname ( string $path )
//> 手册例子 (dirname返回当前目录的上级目录部分)
echo "1) " . dirname("/etc/passwd") . PHP_EOL; // 1) /etc echo "2) " . dirname("/etc/") . PHP_EOL; // 2) / (or \ on Windows) echo "3) " . dirname("."); // 3) . //> __FILE__ 常量和dirname //> __FILE__ 返回当前文件的路径(和inclde|require引用无关) echo dirname(__FILE__); //> 返回当前文件所在路径的上级目录 
  • pathinfo - returns the file path information (array)
# 语法
mixed pathinfo(string $path [,int $options = PATHINFO_DIRNAME | PATHINFO_BASENAME | PATHINFO_EXTENSION | PATHINFO_FILENAME])
//> 返回一个关联数组包含有 path 的信息。返回关联数组还是字符串取决于 options
//> PHPINFO常量
PATHINFO_DIRNAME = 1
PATHINFO_BASENAME = 2 PATHINFO_EXTENSION = 4 PATHINFO_FILENAME = 8 PATHINFO_DIRNAME | PATHINFO_BASENAME | PATHINFO_EXTENSION | PATHINFO_FILENAME = 15 //> 示例 $pathinfo = pathinfo('c:/www/htdocs/inc/lib.inc.php'); print_r($pathinfo); /** Array ( [dirname] => c:/www/htdocs/inc [basename] => lib.inc.php [extension] => php [filename] => lib.inc ) */ //> - - - - - - - - - - - - - - - - - - - $pathinfo = pathinfo('/www/htdocs/inc/'); print_r($pathinfo); /** Array ( [dirname] => /www/htdocs [basename] => inc [filename] => inc ) */ 
  • realpath - Returns canonicalized absolute pathname
# 语法
string realpath ( string $path ) //> 该方法会检查当前文件路径是否存在(不存在时会返回false)
var_dump(realpath('./../../etc/passwd'));      //> false
var_dump(realpath('/windows/system32')); //> c:/windows/system32 windows系统支持自动添加前缀 
  • parse_url - parse URL, return its components
# 语法
mixed parse_url( string $url [,int $component = -1])
//> $component 参数由下面常数组成
PHP_URL_SCHEME => 0
PHP_URL_HOST => 1 [PHP_URL_PORT => 2 PHP_URL_USER => 3 PHP_URL_PASS => 4 PHP_URL_PATH => 5 PHP_URL_QUERY => 6 PHP_URL_FRAGMENT => 7 //> 案例 $url = 'http://username:password@hostname/path?arg=value#anchor'; print_r(parse_url($url)); print_r(parse_url('https://www.sd.tianfumeishi.cn/admin/login/index.php?ts=ts&ns=ns#us')); /** # http://username:password@hostname/path?arg=value#anchor Array ( [scheme] => http [host] => hostname [user] => username [pass] => password [path] => /path [query] => arg=value [fragment] => anchor ) */ /** # https://www.sd.tianfumeishi.cn/admin/login/index.php?ts=ts&ns=ns#us Array ( [scheme] => https [host] => www.sd.tianfumeishi.cn [path] => /admin/login/index.php [query] => ts=ts&ns=ns [fragment] => us ) */ 
  • parse_str - parse the string into a plurality of variables
# 语法
void parse_str ( string $str [, array &$arr ] )
//> 设置了第二个变量 arr,变量将会以数组元素的形式存入到这个数组,作为替代
$str = "first=value&arr[]=foo+bar&arr[]=baz";
parse_str($str);
echo $first.'<br>'; //> value echo $arr[0].'<br>'; //> foo bar echo $arr[1].'<br>'; //> baz //> parse_str 存入第二个参数中 output parse_str($str,$output); echo $output['first'].'<br>'; //> value echo $output['arr'][0].'<br>'; //> foo bar echo $output['arr'][1].'<br>'; //> baz 
  • http_build_query - after the request string generated URL-encode
# 语法
string http_build_query( mixed $query_data [, string ] $numeric_prefix [, string $arg_separator [, int $enc_type ] =PHP_QUERY_RFC1738]]])
//> 参数二:变量前缀(当是数字时添加) | 参数四:编码格式
$data = array(
    'ts' => 'ts', 'ns' => 'ns', 'wd' => 'wd', 'wc' => 'wc' ); echo http_build_query($data) . "\n"; // ts=ts&ns=ns&wd=wd&wc=wc echo http_build_query($data, '', '&a&') . "\n"; // ts=ts&a&ns=ns&a&wd=wd&a&wc=wc echo http_build_query($data, 'wd_', '&a&'); // ts=ts&a&ns=ns&a&wd=wd&a&wc=wc //> 更多用法参看手册




Original: https: //www.jianshu.com/p/729417cc990b

Guess you like

Origin www.cnblogs.com/showcase/p/10935455.html