4 .. versionadded:: 1.10.3
5 The ``split`` filter was added in Twig 1.10.3.
7 The ``split`` filter splits a string by the given delimiter and returns a list
12 {% set foo = "one,two,three"|split(',') %}
13 {# foo contains ['one', 'two', 'three'] #}
15 You can also pass a ``limit`` argument:
17 * If ``limit`` is positive, the returned array will contain a maximum of
18 limit elements with the last element containing the rest of string;
20 * If ``limit`` is negative, all components except the last -limit are
23 * If ``limit`` is zero, then this is treated as 1.
27 {% set foo = "one,two,three,four,five"|split(',', 3) %}
28 {# foo contains ['one', 'two', 'three,four,five'] #}
30 If the ``delimiter`` is an empty string, then value will be split by equal
31 chunks. Length is set by the ``limit`` argument (one character by default).
35 {% set foo = "123"|split('') %}
36 {# foo contains ['1', '2', '3'] #}
38 {% set bar = "aabbcc"|split('', 2) %}
39 {# bar contains ['aa', 'bb', 'cc'] #}
43 Internally, Twig uses the PHP `explode`_ or `str_split`_ (if delimiter is
44 empty) functions for string splitting.
49 * ``delimiter``: The delimiter
50 * ``limit``: The limit argument
52 .. _`explode`: http://php.net/explode
53 .. _`str_split`: http://php.net/str_split