angelopereyra / replace
Replace substring using custom identifiers
v1.1.0
2019-03-26 02:37 UTC
Requires (Dev)
- phpunit/phpunit: 8
- psy/psysh: ^0.9.9
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']