czproject/path-helper

创建相对路径、绝对化路径等辅助类

v4.1.0 2018-06-24 12:07 UTC

This package is auto-updated.

Last update: 2024-09-13 23:44:17 UTC


README

Build Status Downloads this Month Latest Stable Version License

创建相对路径、绝对化路径等辅助类...

Donate

安装

下载最新包 或使用 Composer

composer require czproject/path-helper

库需要 PHP 5.6.0 或更高版本。

用法

use CzProject\PathHelper;

绝对化路径

PathHelper::absolutizePath($path);

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

返回 /path/to/text/file.txt

您可以使用第二个参数 $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

创建相对路径

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

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

返回 ../../imgs/image.jpg

路径当前吗?

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

标准化路径

将路径分隔符标准化为 /

PathHelper::normalizePath($path);

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

返回 /path/to/file.txt

辅助实例

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

许可证: 新BSD许可证
作者: Jan Pecha, https://www.janpecha.cz/