Oihana PHP

relativePath.php

Table of Contents

Functions

relativePath()  : string
Returns the relative path from a base path to a target path.

Functions

relativePath()

Returns the relative path from a base path to a target path.

relativePath(string $path, string $basePath) : string

Both paths must be of the same type (both absolute or both relative), and must share the same root (e.g., same drive letter on Windows). The result is a canonical relative path.

Parameters
$path : string

The target path.

$basePath : string

The base path.

Tags
throws
InvalidArgumentException

If the paths are incompatible (e.g., absolute vs relative, or different roots).

example
echo relativePath( '/foo/bar/baz' , '/foo'     ) . PHP_EOL; // 'bar/baz'
echo relativePath( '/foo/baz'     , '/foo/bar' ) . PHP_EOL; // '../baz'
echo relativePath( '/foo/bar'     , '/foo/bar' ) . PHP_EOL; // '.'
echo relativePath( '/a/b'         , '/a/b/c/d' ) . PHP_EOL; // '../../'
echo relativePath( '/a/b/c'       , '/a'       ) . PHP_EOL; // 'b/c'
echo relativePath( '/a/x/y'       , '/a/b/c'   ) . PHP_EOL; // '../../x/y'

echo relativePath( 'foo/bar/baz'  , 'foo'     ) . PHP_EOL; // 'bar/baz'
echo relativePath( 'foo/baz'      , 'foo/bar' ) . PHP_EOL; // '../baz'
echo relativePath( 'foo/bar'      , 'foo/bar' ) . PHP_EOL; // '.'
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
string

The relative path.


        
On this page

Search results