split
split¶New in version 1.10.3: The split filter was added in Twig 1.10.3.
The split filter splits a string by the given delimiter and returns a list
of strings:
1 2 | {{ "one,two,three"|split(',') }}
{# returns ['one', 'two', 'three'] #}
|
You can also pass a limit argument:
- If
limitis positive, the returned array will contain a maximum of limit elements with the last element containing the rest of string;- If
limitis negative, all components except the last -limit are returned;- If
limitis zero, then this is treated as 1.
1 2 | {{ "one,two,three,four,five"|split(',', 3) }}
{# returns ['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).
1 2 3 4 5 | {{ "123"|split('') }}
{# returns ['1', '2', '3'] #}
{{ "aabbcc"|split('', 2) }}
{# returns ['aa', 'bb', 'cc'] #}
|
delimiter: The delimiterlimit: The limit argument