php常用的路径有关的函数

简单整理一下工作中经常用到的php路径函数

1、basename($path, $suff):返回基本的文件名,如果是以$suff 结束的就去掉

$path = 'E:centosindex.php';
echo basename($path, '.php');  // index
echo basename($path);  // index.php

2、dirname($path)返回路径中的目录部分

$url = 'http://www.baidu.com/abc/index.php?id=1';
echo dirname($url); // http://www.baidu.com/abc

3、realpath($path)返回规范化的绝对路径名

$path = '../phpinfo.php';
echo realpath($path);    // E:wampApache2.2htdocsphpinfo.php

4、pathinfo($path, [int $options])返回文件路径的信息,option 可以指定返回那些单元

$url = 'http://www.baidu.com/abc/index.php?id=1';
$res = pathinfo($url);
echo $res['dirname'];    // http://www.baidu.com/abc
echo $res['basename']; // index.php?id=1
echo $res['extension']; // php?id=1
echo $res['filename']; //  index

5、parse_url($url)解析 URL,返回其组成部分

$url = 'http://www.baidu.com/abc/index.php?id=1#test';
$res = parse_url($url);
echo $res['scheme'];    // http
echo $res['host'];    // www.baidu.com
echo $res['path'];    // /abc/index.php
echo $res['query'];    // 在问号之后的:id=1
echo $res['fragment'];    // 在#之后:test
原文地址:https://www.cnblogs.com/hanpengyu/p/4265627.html