php查看当前的相对路径,php 获取相对路径实例代码

news/2024/7/3 11:53:09
 代码如下复制代码

function getRelativePath($a, $b) {

$returnPath = array(dirname($b));

$arrA = explode('/', $a);

$arrB = explode('/', $returnPath[0]);

for ($n = 1, $len = count($arrB); $n < $len; $n ) {

if ($arrA[$n] != $arrB[$n]) {

break;

}

}

if ($len - $n > 0) {

$returnPath = array_merge($returnPath, array_fill(1, $len - $n, '..'));

}

$returnPath = array_merge($returnPath, array_slice($arrA, $n));

return implode('/', $returnPath);

}

var_dump(getRelativePath($a, $b));

//方法二

$a = "/a/b/c/d/index.php";

//echo basename($a,'.php');// 返回路径中文件名部分

//echo dirname($a);

$b = "/a/b/12/34/index2.php";

function getRelativePath($path1,$path2){

$p1 = dirname($path1);

$p2 = dirname($path2);

$arr1 = explode('/',$p1);

$arr2 = explode('/',$p2);

$diff_arr= array_diff($arr2,$arr1);    //计算差集 注意这里 是谁相对谁的

$inter_arr = array_intersect($arr1,$arr2);//计算交集

$leng = count($inter_arr);    //长度是以交集的为准 即求..部分

for($i=1;$i

$inter_arr[$i] = '..';

}

$merge_arr = array_merge($inter_arr,$diff_arr);

$fina_arr = implode('/',$merge_arr);

return $fina_arr;

}

var_dump(getRelativePath($a,$b));

//返回 string '/../../12/34' (length=12)

//如果改成$diff_arr= array_diff($arr1,$arr2); 就返回string '/../../c/d (length=12)


http://www.niftyadmin.cn/n/3311927.html

相关文章

如何使用异步调用

下面一段文字从别处引用&#xff1a; BeginInvoke方法用于启动异步调用。它与需要异步执行的方法具有相同的参数。此外&#xff0c;它还有两个可选参数。第一个参数是一个AsyncCallback委托&#xff0c;该委托引用在异步调用完成时要调用的方法。第二个参数是一个用户定义的对象…

Protocol Buffer(语言规范)

该系列Blog的内容主体主要源自于Protocol Buffer的官方文档&#xff0c;而代码示例则抽取于当前正在开发的一个公司内部项目的Demo。这样做的目的主要在于不仅可以保持Google文档的良好风格和系统性&#xff0c;同时再结合一些比较实用和通用的用例&#xff0c;这样就更加便于公…

php5 session丢失,关于php5 session 丢失问题

关于php5 session 丢失问题php5 session丢失的解决办法&#xff1a;1、修改“php.ini”配置文件&#xff1b;2、使用“Fecade Session::get()”获取或使用助手函数“session()”&#xff1b;3、重启服务&#xff0c;清除缓存。推荐&#xff1a;《PHP视频教程》think PHP5.1使用…

使用snmp4j监听和发送消息

MultiThreadedTrapReceiver类&#xff1a;用于接收trap消息 Java代码 package com.snmp.trap; import java.io.IOException; import java.net.UnknownHostException; import java.util.Vector; import org.snmp4j.CommandResponder; import org.snmp4j.CommandRespon…

搭建C++编译环境

2019独角兽企业重金招聘Python工程师标准>>> 安装git&#xff0c;g gcc 并且升级到4.9版本&#xff0c;用premake 从vs工程 输出为makefile 等 --安装git sudo add-apt-repository ppa:git-core/ppa sudo apt-get update sudo apt-get install git --安装g sudo ap…

php 登陆程序,PHP(10) 用户登录程序(版本2)

用户登录程序(版本2)今天&#xff0c;我们把我们上一篇博文中的小项目做一个升级版本&#xff01;1、产生彩色的验证码在上一篇文中&#xff0c;我们产生了随机的验证码&#xff0c;但是我们在有的网站上看到的验证码是彩色的&#xff0c;那么&#xff0c;我们怎么来产生彩色的…

【语言处理与Python】3.5正则表达式的有益作用

提取字符块 #找到所有的无重叠的匹配指定的正则表达式re.findall(r’[aeiou]’,word)#看看一些文本中的两个或者两个以上的元音序列&#xff0c;并确定他们的相对频率wsfsorted((set(nltk.corpus.treebank.words()))fdnltk.FreqDist(vs for word in wsjfor vs in re.findall(r’…

yiic.php,执行yiic命令提示’php.exe’将YII添加到系统环境变量

还有个问题就是每次执行yiic命令时要把yiic的全路径打出来才行,比如我的yii框架在F:\myproject里面,我每次执行的时候都要F:\myproject\yii\framework\yiic webapp yiiblog我们可以把yiic也加入系统环境变量,加入以下代码:假如php.exe这个目录在E:\Server\phpF:\myproject\yii\…