Download the PHP package lukaswhite/token-strings without Composer
On this page you can find all versions of the php package lukaswhite/token-strings. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download lukaswhite/token-strings
More information about lukaswhite/token-strings
Files in lukaswhite/token-strings
Package token-strings
Short Description A library for replacing tokens in strings
License MIT
Informations about the package token-strings
Token Strings
This simple library allows you to create a string that includes tokens, for example:
Then provide values for the tokens at runtime:
And run a token substitution:
It's useful for things like page titles, notification templates, mail merge and more.
Installation
Install using Composer:
Basic Usage
Create an instance:
Then run it:
Advanced Usage
You can modify the content of the template at any time:
To add tokens:
To clear the tokens:
If you'd prefer differrnt markup for the tokens:
Since the closing tag in the example above is simply the reverse of the opening tag, you can simply do this:
In addition to passing strings as token values, you can also pass an object, provided it implements the magic __toString()
method:
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
- 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