PHPackages                             lukaswhite/token-strings - 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. lukaswhite/token-strings

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

lukaswhite/token-strings
========================

A library for replacing tokens in strings

1.0(7y ago)015MITPHP

Since Oct 23Pushed 7y ago1 watchersCompare

[ Source](https://github.com/lukaswhite/token-strings)[ Packagist](https://packagist.org/packages/lukaswhite/token-strings)[ RSS](/packages/lukaswhite-token-strings/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (2)Versions (2)Used By (0)

Token Strings
=============

[](#token-strings)

This simple library allows you to create a string that includes tokens, for example:

```
Dear [[FORENAME]] [[SURNAME]],

```

Then provide values for the tokens at runtime:

```
[
	'FORENAME'  =>  'Joe',
	'SURNAME'   =>  'Bloggs',
]
```

And run a token substitution:

```
Dear Joe Bloggs,

```

It's useful for things like page titles, notification templates, mail merge and more.

Installation
------------

[](#installation)

Install using Composer:

```
composer require lukaswhite/token-strings
```

Basic Usage
-----------

[](#basic-usage)

Create an instance:

```
use Lukaswhite\TokenStrings\Substitutor( );

$substitutor = new Substitutor(
	'Dear [[FORENAME]] [[SURNAME]],',
	[
		'forename'  =>  'Joe',
		'surname'   =>  'Bloggs',
	]
);
```

Then run it:

```
$replaced = $substitutor->run( );
// or
$replaced = ( string ) $substitutor;
```

Advanced Usage
--------------

[](#advanced-usage)

You can modify the content of the template at any time:

```
$substitutor->setContent( 'Hey [[FORENAME]]' );
```

To add tokens:

```
$substitutor->addToken( 'age', 43 );
```

To clear the tokens:

```
$substitutor->clearTokens( );
```

If you'd prefer differrnt markup for the tokens:

```
$substitutor->setOpeningTag( '{{' )->setClosingTag( '}}' );
```

Since the closing tag in the example above is simply the reverse of the opening tag, you can simply do this:

```
$substitutor->setOpeningTag( '{{' );
```

In addition to passing strings as token values, you can also pass an object, provided it implements the magic `__toString()` method:

```
class Person {

    private $forename;

    private $surname;

    public function __construct( $forename, $surname )
    {
        $this->forename = $forename;
        $this->surname = $surname;
    }

    public function __toString( )
    {
        return sprintf( '%s %s', $this->forename, $this->surname );
    }
}

$substitutor = new Substitutor(
	'Dear [[NAME]],',
	[
		'NAME'  =>  new Person( 'Joe', 'Bloggs' ),
	]
);
```

To get a list of the available tokens, call `getAvailableTokens()`.

The `validate()` method checks that the string you provide does not contain any tokens for which you haven't provided values.

Notes
-----

[](#notes)

- Tokens must only contain letters, numbers, dashes and underscores
- By convention tokens are uppercase, but if you provide an array with the tokens in lowercase, it'll convert them for you
- If the string contains tokens that you have not provide values for, it will replace them with empty strings

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity61

Established project with proven stability

 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

Unknown

Total

1

Last Release

2761d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/f1bd633ffdf0f66a73a288c4edcacfa43d214cd95a53384c6fe5ea4b3554dd9d?d=identicon)[lukaswhite](/maintainers/lukaswhite)

---

Top Contributors

[![lukaswhite](https://avatars.githubusercontent.com/u/999014?v=4)](https://github.com/lukaswhite "lukaswhite (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/lukaswhite-token-strings/health.svg)

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

###  Alternatives

[winzou/state-machine

A very lightweight yet powerful PHP state machine

52113.7M18](/packages/winzou-state-machine)[ebess/advanced-nova-media-library

Laravel Nova tools for managing the Spatie media library.

6123.3M21](/packages/ebess-advanced-nova-media-library)[elvanto/litemoji

A PHP library simplifying the conversion of unicode, HTML and shortcode emoji.

864.6M8](/packages/elvanto-litemoji)[nextcloud/coding-standard

Nextcloud coding standards for the php cs fixer

143.4M4](/packages/nextcloud-coding-standard)

PHPackages © 2026

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