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

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

webwizardsusa/token-replace
===========================

Define tokens and replace the occurrences in strings.

0.1.1(1y ago)087MITPHPPHP ^8.2

Since Dec 11Pushed 1y ago1 watchersCompare

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

READMEChangelogDependencies (1)Versions (4)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 \Webwizardsusa\TokenReplace\TokenReplacer::from($input)
    ->with('date',  \Webwizardsusa\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:

```
\Webwizardsusa\TokenReplace\TokenReplacer::$defaultTransformers = [
  'date' => \Webwizardsusa\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 \\Webwizardsusa\\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 \\Webwizardsusa\\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

26

—

LowBetter than 43% of packages

Maintenance39

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity43

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~0 days

Total

2

Last Release

523d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/4a47419a2a5ee39e5e903f8b198ac4b9e3f8a0697af50d1f088847b35bdd143a?d=identicon)[webwizardsusa](/maintainers/webwizardsusa)

---

Top Contributors

[![jholly-wwu](https://avatars.githubusercontent.com/u/121579147?v=4)](https://github.com/jholly-wwu "jholly-wwu (2 commits)")

---

Tags

laravelwebwizardsusa

### Embed Badge

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

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

###  Alternatives

[stephenjude/filament-blog

Filament Blog Builder

20317.8k](/packages/stephenjude-filament-blog)[syrian-open-source/laravel-youtube-iframe-generator

Laravel package allows you to generate an iframe tag with a video player depending on a youtube URL.

184.2k](/packages/syrian-open-source-laravel-youtube-iframe-generator)

PHPackages © 2026

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