Andrew Welch
Published , updated · 5 min read · RSS Feed
Please consider 🎗 sponsoring me 🎗 to keep writing articles like this.
Advanced String Manipulation in Twig with Craft CMS
Learn how to unleash hidden string manipulation powers in Twig with Craft CMS 5, no plugins required!
While Twig has some useful functions & filters for string manipulation and interpolation, sometimes you need to do more advanced string manipulation than what it offers out of the box.
You might turn to a plugin like Typogrify or Wordsmith to help you out with your string manipulation needs. And while both plugins are quite useful, and do far more than just string manipulation, you may find out that you don’t need them at all!
This article will show you how to tap into powerful string manipulation that is already baked into Twig when using Craft CMS.
No plugins required!
Link create() some Stringy
Since Craft CMS 3, a PHP library called Stringy has been bundled with Craft and is used internally for many string manipulation functions.
Stringy offers hundreds of helpful string manipulation functions that are sitting there just waiting for you to use them. The question is how do you tap into them from Twig?
The answer is the create() function, which has been available in Twig since Craft CMS 3.1.6:
{% set someString = 'This is my string. There are many like it, but this one is mine.' %}
{# @var someStringy \Stringy\Stringy #}
{% set someStringy = create('Stringy\\Stringy', [someString]) %}
The code above creates a new Stringy PHP object from the Twig variable someString. The first parameter to create() is the class to create, and the second is an array of parameters to pass in when creating it.
In our case, we just pass in the someString variable, and the Stringy class is returned to us in the someStringy variable.
This opens up hundreds of Stringy functions we can now perform on our string!
N.B.: The {# @var someStringy \Stringy\Stringy #} Twig comment is a type-hint that will give you autocomplete if you’re using PhpStorm:
Link A Taste of Stringy
Let’s have a look at a few useful examples of what you can do with your newfound powers. This is just a small taste of what you can do, as there are hundreds of Stringy functions available to you.
slugify()
{% set someString = 'This is my string. There are many like it, but this one is mine.' %}
{# @var someStringy \Stringy\Stringy #}
{% set someStringy = create('Stringy\\Stringy', [someString]) %}
{{ someStringy.slugify() }}
Outputs:
this-is-my-string-there-are-many-like-it-but-this-one-is-mine
titleizeForHumans()
{% set someString = 'This is my string. There are many like it, but this one is mine.' %}
{# @var someStringy \Stringy\Stringy #}
{% set someStringy = create('Stringy\\Stringy', [someString]) %}
{{ someStringy.titleizeForHumans() }}
Outputs:
This Is My String. There Are Many Like It, but This One Is Mine.
longestCommonSubstring()
{% set someString = 'This is my string. There are many like it, but this one is mine.' %}
{# @var someStringy \Stringy\Stringy #}
{% set someStringy = create('Stringy\\Stringy', [someString]) %}
{{ someStringy.longestCommonSubstring('but this one is not mine') }}
Outputs:
but this one is
safeTruncate()
{% set someString = 'This is my string. There are many like it, but this one is mine.' %}
{# @var someStringy \Stringy\Stringy #}
{% set someStringy = create('Stringy\\Stringy', [someString]) %}
{{ someStringy.safeTruncate(25, '…') }}
Outputs:
This is my string. There…
Link String it all together
The examples presented here are just the tip of the iceberg, explore all of the functions available in Stringy to see what else you can do… all without a plugin.
You may be wondering whether the aforementioned plugins Typogrify and Wordsmith somehow do these things better? Nope, turns out they do it in the same way, using Stringy themselves!
Happy stringing!