ashleydawson/globtoregex

将glob模式转换为正则表达式的函数,来自Symfony Finder组件

1.0.0 2019-06-24 08:51 UTC

This package is auto-updated.

Last update: 2024-09-25 06:59:09 UTC


README

Build Status

PHP函数,将glob模式转换为正则表达式,来自Symfony Finder组件。

安装

要使用Composer安装,请执行以下步骤

$ composer req ashleydawson/globtoregex

要求如下

  • PHP >= 7.1

基本用法

函数的基本用法如下

<?php

require __DIR__.'/vendor/autoload.php';

$regex = \AshleyDawson\GlobToRegex\glob_to_regex('/**/*.txt');

echo $regex;

输出结果为

#^(?=[^\.])/(?:(?=[^\.])[^/]++/)*(?=[^\.])[^/]*\.txt$#

用法

以下简单示例返回匹配的文件路径集

<?php

require __DIR__.'/vendor/autoload.php';

use function AshleyDawson\GlobToRegex\glob_to_regex;

$paths = [
    '/foo/bar/sample.txt', 
    '/baz/biz/example.txt', 
    '/fiz/boo/music.mp3',
];

// Find matches for the glob pattern `/**/*.txt`
$regex = glob_to_regex('/**/*.txt');

$matches = array_filter($paths, function ($path) use ($regex) {
    return preg_match($regex, $path);
});

print_r($matches);

输出结果为

Array
(
    [0] => /foo/bar/sample.txt
    [1] => /baz/biz/example.txt
)

测试

要运行测试套件,请执行以下步骤

$ vendor/bin/phpunit -c .