Edit on GitHub
Jump to docs navigation

Twig Components / Methods / split

Note: You are currently reading the documentation for Bolt 5.0. Looking for the documentation for Bolt 5.2 instead?

split(delimiter, limit = null) is a Twig filter to split a string by the given delimiter and returns a list of strings:

{% set foo = "one,two,three"|split(',') %}
{# foo contains ['one', 'two', 'three'] #}

You can also pass a limit argument:

  • If limit is positive, the returned array will contain a maximum of limit elements with the last element containing the rest of string;
  • If limit is negative, all components except the last -limit are returned;
  • If limit is zero, then this is treated as 1.
{% set foo = "one,two,three,four,five"|split(',', 3) %}
{# foo contains ['one', 'two', 'three,four,five'] #}

If the delimiter is an empty string, then value will be split by equal chunks. Length is set by the limit argument (one character by default).

{% set foo = "123"|split('') %}
{# foo contains ['1', '2', '3'] #}

{% set bar = "aabbcc"|split('', 2) %}
{# bar contains ['aa', 'bb', 'cc'] #}

Note: Internally, Twig uses the PHP explode or str_split (if delimiter is empty) functions for string splitting.

ArgumentsΒΆ

  • delimiter: The delimiter
  • limit: The limit argument

Source: Twig



Edit this page on GitHub
Couldn't find what you were looking for? We are happy to help you in the forum, on Slack or on Github.