PHP Get the relative path of the file file_a relative to file_b


<?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;
}
 
?>


{{o.name}}
{{m.name}}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324136453&siteId=291194637