jackiedo/path-helper

PHP操作本地路径的辅助类

v1.0.0 2022-03-07 20:28 UTC

This package is auto-updated.

Last update: 2024-09-17 11:52:58 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License

PHP操作本地路径的辅助类。

兼容性

此包需要PHP 5.4.0或更高版本。

概览

查看以下主题以了解有关Path Helper的更多信息

安装

下载最新包或使用Composer

$ composer require jackiedo/path-helper

使用

在需要composer自动加载器后,您可以使用以下方式使用此包

使用静态方法

use Jackiedo\PathHelper\Path;

// ...

$return = Path::doSomething();

使用实例方法

use Jackiedo\PathHelper\Path;

// ...

$helper = new Path;
$return = $helper->doSomething();

使用内置函数

有关更多详细信息,请参阅此处

可用方法

规范化路径的目录分隔符

使用特定字符串格式化给定路径的目录分隔符。

语法

/**
 * Formats the directory separators of a given path with a specific string.
 *
 * @param string $path      the path want to normalize
 * @param string $separator the directory separator want to use
 *
 * @return string
 */
public static function normalize($path, $separator = DIRECTORY_SEPARATOR);

示例

$return1 = Path::normalize('path\\to/specific/file/or\\directory');
// The result returned will depend on the operating system
//     On Windows -> path\to\specific\file\or\directory
//     On Unix    -> path/to/specific/file/or/directory

$return2 = Path::normalize('path\\to/specific/file/or\\directory', '/');
// path/to/specific/file/or/directory

$return3 = Path::normalize('path\\to/specific/file/or\\directory', ' > ');
// path > to > specific > file > or > directory

重排路径

重排为Unix风格

normalize($path, '/')方法的替代方法。

语法

/**
 * Normalize directory separators of a given path according to Unix OS style.
 *
 * @param string $path the path want to normalize
 *
 * @return string
 */
public static function unixStyle($path);

示例

$return = Path::unixStyle('path\\to/specific/file/or\\directory');
// path/to/specific/file/or/directory

重排为Windows风格

normalize($path, '\\')方法的替代方法。

语法

/**
 * Normalize directory separators of a given path according to Windows OS style.
 *
 * @param string $path the path want to normalize
 *
 * @return string
 */
public static function winStyle($path);

示例

$return = Path::winStyle('path\\to/specific/file/or\\directory');
// path\to\specific\file\or\directory

重排为当前操作系统风格

normalize($path, DIRECTORY_SEPARATOR)方法的替代方法。

语法

/**
 * Normalize directory separators of a given path according to the current OS style.
 *
 * @param string $path the path want to normalize
 *
 * @return string
 */
public static function osStyle($path);

示例

$return = Path::osStyle('path\\to/specific/file/or\\directory');
// The result returned will depend on the operating system
//     On Windows -> path\to\specific\file\or\directory
//     On Unix    -> path/to/specific/file/or/directory

创建绝对路径

从给定路径创建绝对路径。

语法

/**
 * Return absolute path from a given path.
 *
 * This method is an alternative to `realpath()` function for non-existent paths.
 *
 * @param string $path      the path want to format
 * @param string $separator the directory separator want to use in the result
 *
 * @return string
 */
public static function absolute($path, $separator = DIRECTORY_SEPARATOR);

示例

$return = Path::absolute('./this\\is/../sample/path');
// The result returned will depend on the operating system and current working directory
// You will probably get the following result: /home/www/public_html/this/sample/path

注意

乍一看,此方法看起来像PHP的realpath()函数,但实际上它以不同的方式工作。

realpath()函数返回到现有目录或文件的绝对路径,而此方法不检查实际的存在性。

创建相对路径

从给定文件或目录创建到另一个位置的相对路径。

语法

/**
 * Return relative path from a given file or directory to another location.
 *
 * @param string $from      the path of departure file or directory
 * @param string $to        the path of destination file or directory
 * @param string $separator the directory separator want to use in the result
 *
 * @return string
 */
public static function relative($from, $to, $separator = DIRECTORY_SEPARATOR);

示例

$return = Path::absolute('./this\\is/../sample/path', '/home/www/another/directory');
// The result returned will depend on the operating system and current working directory
// You will probably get the following result: ../../../../another/directory

检查路径格式

检查绝对格式

语法

/**
 * Check if a given path is an absolute path.
 *
 * @param string $path the path want to check
 *
 * @return bool
 */
public static function isAbsolute($path);

示例

$return = Path::isAbsolute('/home/www/public_html');      // true
$return = Path::isAbsolute('sample/../path');             // false
$return = Path::isAbsolute('D:\\home\\www\\public_html'); // true
$return = Path::isAbsolute('sample\\..\\path');           // false

检查相对格式

语法

/**
 * Check if a given path is a relative path.
 *
 * @param string $path the path want to check
 *
 * @return bool
 */
public static function isRelative($path);

示例

$return = Path::isRelative('/home/www/public_html');      // false
$return = Path::isRelative('sample/../path');             // true
$return = Path::isRelative('D:\\home\\www\\public_html'); // false
$return = Path::isRelative('sample\\..\\path');           // true

检查路径的相互包裹

检查路径是否为另一个路径的祖先

语法

/**
 * Check if a given path is ancestor of the another path.
 *
 * Return true if the input path is ancestor of the comparison
 *
 * @param string $path       the path want to check
 * @param string $comparison the target path used for comparison
 *
 * @return bool
 */
public static function isAncestor($path, $comparison);

示例

$return = Path::isAncestor('/home/www', '/home/www/public/assets/css/../images/sample.png');     // true
$return = Path::isAncestor('/home/www', '/another_home/public/assets/css/../images/sample.png'); // false

检查路径是否为另一个路径的后代

语法

/**
 * Check if a given path is descendant of the another path.
 *
 * Return true if the input path is descendant of the comparison
 *
 * @param string $path       the path want to check
 * @param string $comparison the target path used for comparison
 *
 * @return bool
 */
public static function isDescendant($path, $comparison);

示例

$return = Path::isDescendant('/home/www/public/assets/css/../images/sample.png', '/home/www');     // true
$return = Path::isDescendant('/another_home/public/assets/css/../images/sample.png', '/home/www'); // false

可用函数

此包包含几个内置函数,作为使用Path方法的替代方案。然而,不建议使用这些函数,因为它们可能与其他包的函数冲突。

许可证

MIT © Jackie Do