Home > Backend Development > PHP Tutorial > php中获取资料file_a相对于文件file_b的目录

php中获取资料file_a相对于文件file_b的目录

WBOY
Release: 2016-06-13 12:41:41
Original
843 people have browsed it

php中获取文件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);
    $r_path = dirname($r_path);
    return  $r_path;
}

$file_a='/a/66/bcdd/44/app/index.php';
$file_b='/a/66/bcdf/goods.php';

echo getFileRelativePath($file_a, $file_b);
Copy after login

?

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template