weew/url-matcher
最新稳定版本:v1.1.0
Composer 安装命令:
composer require weew/url-matcher
包简介
Simple url matcher and parser.
README 文档
README
Table of contents
Installation
composer require weew/url-matcher
Introduction
This this simple matcher allows you to match url like paths against patterns with placeholders and even extract them.
Usage
Creating a new matcher is very simple.
$matcher = new UrlMatcher();
Matching
Below is a very basic matching example.
// true $matcher->match('users/{id}', 'users/1'); // false $matcher->match('users/{id}', 'users');
Placeholders can be optional by adding ? at the end.
// true $matcher->match('users/{id?}', 'users/1'); // true $matcher->match('users/{id?}', 'users');
Placeholders can have custom patterns.
$matcher->addPattern('id', '[0-9]+'); // true $matcher->match('users/{id}', 'users/1'); // false $matcher->match('users/{id}', 'users/abc');
You can provide patterns inline.
// true $matcher->match('users/{id}', 'users/1', [ 'id' => '[0-9]+', ]);
Placeholders can be optional too.
// true $matcher->match('users/{id?}', 'users/1', [ 'id' => '[0-9]+', ]); // true $matcher->match('users/{id?}', 'users', [ 'id' => '[0-9]+', ]);
Parsing
Extracting placeholders is very trivial too. The parse method always returns an instance of IDictionary.
$dictionary = $matcher->parse('users/{id}', 'users/123'); // 123 $dictionary->get('id'); $dictionary = $matcher->parse('users/{id}', 'users'); // null $dictionary->get('id');
Of course, placeholders can have custom patterns.
$matcher->addPattern('id', '[0-9]+'); $dictionary = $matcher->parse('users/{id}', 'users/123'); // 123 $dictionary->get('id'); $dictionary = $matcher->parse('users/{id}', 'users/abc'); // null $dictionary->get('id');
Replacing
You can do the opposite of parsing by replacing placeholders with specific values.
// api.service.com $matcher->replace('{subdomain}.service.com', 'subdomain', 'api'); // api.service.com/v1 $matcher->replaceAll('{subdomain}.service.com/{version}', ['subdomain' => 'api', 'version' => 'v1']);
统计信息
- 总下载量: 50.45k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-07-16