PHPackages                             jesseschutt/token-replacer - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. jesseschutt/token-replacer

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

jesseschutt/token-replacer
==========================

Define tokens and replace the occurrences in strings.

v3.0.0(1y ago)03.0k—2.4%MITPHPPHP ^8.2CI passing

Since Aug 16Pushed 1y agoCompare

[ Source](https://github.com/jesseschutt/token-replacer)[ Packagist](https://packagist.org/packages/jesseschutt/token-replacer)[ Docs](https://github.com/jesseschutt/token-replacer)[ RSS](/packages/jesseschutt-token-replacer/feed)WikiDiscussions develop Synced 2d ago

READMEChangelog (3)Dependencies (4)Versions (5)Used By (0)

Token Replacer
==============

[](#token-replacer)

[![Build Status](https://github.com/jesseschutt/token-replacer/actions/workflows/run-tests.yml/badge.svg)](https://github.com/jesseschutt/token-replacer/actions/workflows/run-tests.yml/badge.svg)[![Code Coverage](https://camo.githubusercontent.com/38b28fdfc991596d7f596ea5f2d89727eeec343fb118b125a4c1f3ecb1e1343e/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f6a657373657363687574742f746f6b656e2d7265706c61636572)](https://camo.githubusercontent.com/38b28fdfc991596d7f596ea5f2d89727eeec343fb118b125a4c1f3ecb1e1343e/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f6a657373657363687574742f746f6b656e2d7265706c61636572)[![Dependencies](https://camo.githubusercontent.com/9c391b2b93b2cd760426422fcca7b7cbc57d65bc0e74062d72013db695dc4fd1/68747470733a2f2f696d672e736869656c64732e696f2f6c6962726172696573696f2f6769746875622f6a657373657363687574742f746f6b656e2d7265706c61636572)](https://camo.githubusercontent.com/9c391b2b93b2cd760426422fcca7b7cbc57d65bc0e74062d72013db695dc4fd1/68747470733a2f2f696d672e736869656c64732e696f2f6c6962726172696573696f2f6769746875622f6a657373657363687574742f746f6b656e2d7265706c61636572)

### Credit

[](#credit)

This package is a fork of the [original](https://github.com/HollyIT/token-replacer), which was created by Jamie Holly of HollyIT.

### Installation

[](#installation)

```
composer require jesseschutt/token-replacer

```

### Configuration

[](#configuration)

You can publish the configuration file by running the following command:

```
php artisan vendor:publish --provider="JesseSchutt\TokenReplacer\TokenReplacerServiceProvider"

```

This will create a `token-replacer.php` file in your `config` directory. Here you can set the default token start and end characters, the default token separator, and the default transformers.

### Instructions

[](#instructions)

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

```
use \JesseSchutt\TokenReplacer\Facades\TokenReplacer;
use \JesseSchutt\TokenReplacer\Transformers\DateTransformer;

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

echo TokenReplacer::from($input)->with('date', 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`config('token-replacer.default_start_token')`$instance-&gt;startToken()dateToken Name------:Token Separator`config('token-replacer.default_ token_separator')`$instance-&gt;tokenSeparator()mOptions------}}Token End`config('token-replacer.default_end_token')`$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 adding them to the `default_transformers` array in the configuration file.

**Note:** Global transformers do not receive any data when instantiated. `DateTransformer` and `AuthTransformer` are the only two built-in transformers that are eligible for global use.

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

```
function(string $options)

```

#### Included Transformers

[](#included-transformers)

All these transformers live under the `\JesseSchutt\TokenReplacer\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 `\JesseSchutt\TokenReplacer\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.

###  Health Score

36

—

LowBetter than 79% of packages

Maintenance45

Moderate activity, may be stable

Popularity21

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 83.3% 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 ~128 days

Total

3

Last Release

429d ago

Major Versions

v1.0.0 → v2.0.02024-08-17

v2.0.0 → v3.0.02025-05-01

### Community

Maintainers

![](https://www.gravatar.com/avatar/1ffae66f8a3c9ea283079e6e74e520b39fd68a00dab4485aee819a69c0f3fa25?d=identicon)[jesseschutt](/maintainers/jesseschutt)

---

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 (5 commits)")

---

Tags

laraveltoken-replacement

###  Code Quality

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/jesseschutt-token-replacer/health.svg)

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

###  Alternatives

[grumpydictator/firefly-iii

Firefly III: a personal finances manager.

23.9k69.5k](/packages/grumpydictator-firefly-iii)[firefly-iii/data-importer

Firefly III Data Import Tool.

8035.8k](/packages/firefly-iii-data-importer)[markwalet/nova-modal-response

A Laravel Nova asset for Modal responses on an action.

17878.9k](/packages/markwalet-nova-modal-response)[ronasit/laravel-helpers

Provided helpers function and some helper class.

2085.6k31](/packages/ronasit-laravel-helpers)[team-nifty-gmbh/tall-datatables

Server-side rendered datatables for Laravel and Livewire

1320.9k4](/packages/team-nifty-gmbh-tall-datatables)[tomshaw/electricgrid

A feature-rich Livewire package designed for projects that require dynamic, interactive data tables.

119.4k](/packages/tomshaw-electricgrid)

PHPackages © 2026

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