Convert paths relative from 1 file to another
Clone or download
matthiasmullie Allow specifying root directory to start comparing from
This library was originally built mostly for web purposes,
where an absolute path would usually be the server's root
directory (e.g. /var/www).
There's some assumptions built in that have their basis
there, like how it'll append the paths to the current
working directory.
That makes it hard to use this library for cli based
transformations, though, where the root would usually be
'/'  instead of cwd...
There's now an optional 3rd argument that can be passed to
the constructor to inform it the root is something else
(e.g. '/')
Latest commit 5e4b121 Oct 26, 2018
Type Name Latest commit message Commit time
Failed to load latest commit information.
src Allow specifying root directory to start comparing from Oct 25, 2018
tests Fix #4 Oct 25, 2018
.gitattributes Replace coveralls by codecov Jan 14, 2016
.gitignore User specific files should not appear in .gitignore Jan 25, 2016
.travis.yml Test on PHP 7.0 & 7.1 Jan 26, 2017
LICENSE Init Feb 20, 2015 Replace coveralls by codecov Jan 14, 2016
composer.json Replace coveralls by codecov Jan 14, 2016
makefile Init Feb 20, 2015
phpunit.xml.dist Init Feb 20, 2015

Path converter

Build status Code coverage Code quality Latest version Downloads total License


use MatthiasMullie\PathConverter\Converter;

$from = '/css/imports/icons.css';
$to = '/css/minified.css';

$converter = new Converter($from, $to);
$result = $converter->convert('../../images/icon.jpg');
// $result is now '../images/icon.jpg'


__construct($from, $to)

The object constructor accepts 2 paths: the source path your file(s) is/are currently relative to, and the target path to convert to.

convert($path): string

$path is the relative file, which is currently relative to $from (in constructor). The return value will be the relative path of this same file, but now relative to $to (in constructor)


Simply add a dependency on matthiasmullie/path-converter to your composer.json file if you use Composer to manage the dependencies of your project:

composer require matthiasmullie/path-converter

Although it's recommended to use Composer, you can actually include these files anyway you want.


PathConverter is MIT licensed.