CzProject\PathHelper
, (*1)
Helper class for creating relative paths, absolutizing paths,..., (*2)
, (*3)
Installation
Download a latest package or use Composer:, (*4)
composer require czproject/path-helper
Library requires PHP 5.6.0 or later., (*5)
Usage
``` php
use CzProject\PathHelper;, (*6)
### Absolutize path
``` php
PathHelper::absolutizePath($path);
PathHelper::absolutizePath('path/to/my/../text/./file.txt');
Returns /path/to/text/file.txt
, (*7)
You can use second parameter $prefix
:, (*8)
``` php
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, (*9)
### Creating relative path
``` php
$source = 'root/dir/docs/1.0/index.html';
$dest = 'root/dir/imgs/image.jpg';
PathHelper::createRelativePath($source, $dest);
Returns ../../imgs/image.jpg
, (*10)
Is path current?
``` php
PathHelper::isPathCurrent($path, $mask);, (*11)
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, (*12)
| Mask | Meaning
| -------- | ------------------------------------------
| ```**``` | means *everything*
| ```*``` | means *everything <b>except</b> ```/```*
### Normalize path
Normalizes path delimiters to `/`.
``` php
PathHelper::normalizePath($path);
PathHelper::normalizePath('\\path\\to\\file.txt');
Returns /path/to/file.txt
., (*13)
Helper instance
php
$helper = new CzProject\PathHelper;
$helper->absolutizePath('/path/to/to/../file');
$helper->createRelativePath('/path/to/file', '/path/to');
$helper->isPathCurrent('/path/file', '/path/*');
, (*14)
License: New BSD License
Author: Jan Pecha, https://www.janpecha.cz/, (*15)