Heim  >  Artikel  >  Backend-Entwicklung  >  php中获取资料file_a相对于文件file_b的目录

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

WBOY
WBOYOriginal
2016-06-13 12:41:41794Durchsuche

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);

?

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn