Skip to content

Helper class for creating relative paths, absolutizing paths,…

License

Notifications You must be signed in to change notification settings

czproject/path-helper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CzProject\PathHelper

Build Status Downloads this Month Latest Stable Version License

Helper class for creating relative paths, absolutizing paths,...

Donate

Installation

Download a latest package or use Composer:

composer require czproject/path-helper

Library requires PHP 5.6.0 or later.

Usage

use CzProject\PathHelper;

Absolutize path

PathHelper::absolutizePath($path);

PathHelper::absolutizePath('path/to/my/../text/./file.txt');

Returns /path/to/text/file.txt

You can use second parameter $prefix:

PathHelper::absolutizePath('path/to/my/../text/./file.txt', NULL); // returns path/to/text/file.txt
PathHelper::absolutizePath('path/to/my/../text/./file.txt', '/file/root/'); // returns /file/root/path/to/text/file.txt

Creating relative path

$source = 'root/dir/docs/1.0/index.html';
$dest = 'root/dir/imgs/image.jpg';

PathHelper::createRelativePath($source, $dest);

Returns ../../imgs/image.jpg

Is path current?

PathHelper::isPathCurrent($path, $mask);

PathHelper::isPathCurrent('dir/file.txt', 'dir/file-2.txt'); // returns FALSE
PathHelper::isPathCurrent('dir/file.txt', 'dir/*'); // returns TRUE
PathHelper::isPathCurrent('dir/sub/file.txt', 'dir/*'); // returns FALSE
PathHelper::isPathCurrent('dir/sub/file.txt', 'dir/*/*'); // returns TRUE
PathHelper::isPathCurrent('dir/sub/file.txt', 'dir/**'); // returns TRUE
Mask Meaning
** means everything
* means everything except /

Normalize path

Normalizes path delimiters to /.

PathHelper::normalizePath($path);

PathHelper::normalizePath('\\path\\to\\file.txt');

Returns /path/to/file.txt.

Helper instance

$helper = new CzProject\PathHelper;
$helper->absolutizePath('/path/to/to/../file');
$helper->createRelativePath('/path/to/file', '/path/to');
$helper->isPathCurrent('/path/file', '/path/*');

License: New BSD License
Author: Jan Pecha, https://www.janpecha.cz/

About

Helper class for creating relative paths, absolutizing paths,…

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published