PHPackages                             hollyit/token-replace - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. hollyit/token-replace

ActiveLibrary

hollyit/token-replace
=====================

Define tokens and replace the occurrences in strings.

1.0.2(3y ago)24.2k1MITPHPPHP ^8.1

Since Nov 11Pushed 1y ago2 watchersCompare

[ Source](https://github.com/HollyIT/token-replacer)[ Packagist](https://packagist.org/packages/hollyit/token-replace)[ Docs](https://github.com/hollyit/token-replace)[ RSS](/packages/hollyit-token-replace/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (1)Versions (5)Used By (0)

Token Replace
=============

[](#token-replace)

This simple package allows you to define tokens that can be replaced in strings. Instead of a simple str\_replace, Token Replace lets you add options to each token. Let's start with an example.

```
$input="Today is { date:m }}/{{ date:d }}/{{ date:y }}.";

echo \HollyIT\TokenReplace\TokenReplacer::from($input)
    ->with('date',  \HollyIT\TokenReplace\Transformers\DateTransformer::class)

// Results in: Today is 11/11/21.

```

There is a certain anatomy to tokens, so let's take a look
at the {{ date:m }}. This is a default token format, but
this format is configurable globally and per instance.

PartNameGlobal SettingLocal Setting{{Token Start$defaultStartToken$instance-&gt;startToken()dateToken Name------:Token Separator$defaultTokenSeparator$instance-&gt;tokenSeparator()mOptions------}}Token End$defaultEndToken$instance-&gt;endToken()### Transformers

[](#transformers)

The replacement of tokens is handled via a transformer. A transformer can be a closure or a simple class.

Transformers can be added to each instance of the TokenReplacer or added globally by executing the following in the bootstrap phase of your app:

```
\HollyIT\TokenReplace\TokenReplacer::$defaultTransformers = [
  'date' => \HollyIT\TokenReplace\Transformers\DateTransformer::class
];

```

Per instance tokens are added via $instance-&gt;with({token name}, {class name, transformer instance or closure}); For closure based transformers the signature is:

```
function(string $options, TokenReplacer $replacer)

```

#### Included Transformers

[](#included-transformers)

All these transformers live under the \\HollyIT\\TokenReplace\\Transformers namespace.

TransformerDescriptionInstantiatingArrayTransformerExtracts a value from an array based on a key.The arrayDateTransfomerExtract parts of a date by [PHP date format](https://www.php.net/manual/en/datetime.format.php) tokens.Date object, Carbon, date string or null (assumes now())FileTransformerExtract parts from a file based on pathinfo().The path stringObjectTransformerExtract a property from an object.The objectUrlTransformerExtract based on parse\_url()The URL stringThere are also special Laravel transformers residing in \\HollyIT\\TokenReplace\\Transformers\\Laravel

TransformerDescriptionInstantiatingAuthTransformerExtract a property from the auth (user) objectnoneDotArrayTransformerExtract a property using Laravel's dot syntaxthe arrayModelTransformerExtract a property from an Eloquent model. For date properties, you can add the date part by specifying the format after the property name (ie: model:created\_at,m)The modelUploadFileTransformerExtract the basename or extension from an UploadedFile objectThe upload### Post processing.

[](#post-processing)

None of the transformers do any further processing except extracting the item. If you want to do further processing, such as escaping the replacements, you can define an onReplace callback:

```
$replacer->onReplace(function(string $result, string $token, string $options){
  return strtoupper($result);
});

```

This callback will run for every token occurrence found, so you can further filter down what tokens to operate on by checking the $token property.

### Invalid or missing tokens

[](#invalid-or-missing-tokens)

By default invalid and missing tokens will remain in the string. To prevent this, you can set removeEmpty() on the TokenReplacer.

### Future Plans

[](#future-plans)

This project was born out of a repeated scenario I faced on numerous projects. There are no real future plans for it. The entire objective here is a very simple solution to a common problem. You may find a transformer or feature that should be included. If so, please submit a PR. I'm very open on extending the project to cover more use cases.

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance29

Infrequent updates — may be unmaintained

Popularity21

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 75.8% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~238 days

Total

3

Last Release

1164d ago

PHP version history (2 changes)1.0.0PHP ^8.0

1.0.2PHP ^8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/58617d67178941252c10067cd88583a4f1773569b630372930c6412fbc939979?d=identicon)[HollyIT](/maintainers/HollyIT)

---

Top Contributors

[![jesseschutt](https://avatars.githubusercontent.com/u/353047?v=4)](https://github.com/jesseschutt "jesseschutt (25 commits)")[![HollyIT](https://avatars.githubusercontent.com/u/527984?v=4)](https://github.com/HollyIT "HollyIT (8 commits)")

---

Tags

laravelhollyit

### Embed Badge

![Health badge](/badges/hollyit-token-replace/health.svg)

```
[![Health](https://phpackages.com/badges/hollyit-token-replace/health.svg)](https://phpackages.com/packages/hollyit-token-replace)
```

###  Alternatives

[orchestra/testbench

Laravel Testing Helper for Packages Development

2.2k39.1M32.0k](/packages/orchestra-testbench)[laravel/browser-kit-testing

Provides backwards compatibility for BrowserKit testing in the latest Laravel release.

5139.4M285](/packages/laravel-browser-kit-testing)[timacdonald/log-fake

A drop in fake logger for testing with the Laravel framework.

4235.9M54](/packages/timacdonald-log-fake)[hollyit/filament-static-assets

Improved handling of stylesheets and javascript files for Filament

1512.9k](/packages/hollyit-filament-static-assets)[soulcodex/laravel-behat

Laravel Extension for easy integration with Behat

1760.8k](/packages/soulcodex-laravel-behat)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
