angelopereyra/replace
Composer 安装命令:
composer require angelopereyra/replace
包简介
Replace substring using custom identifiers
README 文档
README
Installation
> composer require angelopereyra/replace
Usage
use Replace\Replaceable; ... $url = new Replaceable('https://mysite.{domain}.com');
From the above example, we can set the value for {domain} substring in two ways:
$url->addLookup('domain', 'local'); // dumping a dictionary $url->setLookup([ 'domain' => 'local' ]);
Then cast the object to string to get the result
echo (string)$url; // prints "https://mysite.local.com"
Modify the token format
Pass a string as a second argument to the constructor to customize what your token would look like:
$url = new Replaceable('https://mysite.$$domain.com', '$$++key++'); // ++key++ is the 🔑
In some cases you might just need to pass a callable:
$url = new Replaceable('https://mysite.UwU domain UwU.com', function($key) { return 'UwU ' . $key . ' UwU'; });
A static helper is also exposed for convenience
$lookup = [ 'type' => 'Bearer', 'token' => 'xoxb-83029aurioDnd' ]; Replaceable::parse('Authorization: @type @token', $lookup, '@++key++') // prints "Authorization: Bearer xoxb-83029aurioDnd"
Identify tokens
Suppose you have the subject string below:
Hello, {name}. You look {adjective} today.
Replaceable can identify the tokens according to the given token format. In this case, we will be using the default format which is {++key++}.
We can identify the tokens using the following usage:
$subject = file_get_contents('...'); $sentence = new Replaceable($subject); $sentence->identifyTokens(false); // returns ['name', 'adjective']
To identify tokens with respect to word boundary, set the 1st parameter to true:
$sentence->identifyTokens(true); // returns ['adjective']
统计信息
- 总下载量: 14
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-02-08