您的位置:首页 > 编程语言 > PHP开发

PHP 获取 文件file_a相对于file_b的相对路径

2017-03-15 00:00 399 查看
<?php
$file_a='/a/66/bcdd/44/app/index.php';
$file_b='/a/66/bcdf/goods.php';

echo getFileRelativePath($file_a, $file_b);
/**
* @desc 获取 file_a相对于file_b的相对路径
* @author Jihayang
* @return string
*/
function getFileRelativePath($file_a, $file_b){
#$file_a^$file_b 按位或运算(数学中称异或),将字符串转为16进制,相同为0,否则为1,
#查找$file_a^$file_b里连续等于\x00直到不等于\x00后结束,并返回当前的位置,
$pos = strspn($file_a^$file_b,"\x00");
#输出多少个上级相同目录../
$r_path = str_repeat('../', substr_count($file_a,'/',$pos));
#输出相对路径
$r_path .= substr($file_b,strrpos(substr($file_b,0,$pos), '/')+1);
return  $r_path;
}

?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: