1``split`` 2========= 3 4.. versionadded:: 1.10.3 5 The ``split`` filter was added in Twig 1.10.3. 6 7The ``split`` filter splits a string by the given delimiter and returns a list 8of strings: 9 10.. code-block:: jinja 11 12 {% set foo = "one,two,three"|split(',') %} 13 {# foo contains ['one', 'two', 'three'] #} 14 15You can also pass a ``limit`` argument: 16 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; 19 20* If ``limit`` is negative, all components except the last -limit are 21 returned; 22 23* If ``limit`` is zero, then this is treated as 1. 24 25.. code-block:: jinja 26 27 {% set foo = "one,two,three,four,five"|split(',', 3) %} 28 {# foo contains ['one', 'two', 'three,four,five'] #} 29 30If the ``delimiter`` is an empty string, then value will be split by equal 31chunks. Length is set by the ``limit`` argument (one character by default). 32 33.. code-block:: jinja 34 35 {% set foo = "123"|split('') %} 36 {# foo contains ['1', '2', '3'] #} 37 38 {% set bar = "aabbcc"|split('', 2) %} 39 {# bar contains ['aa', 'bb', 'cc'] #} 40 41.. note:: 42 43 Internally, Twig uses the PHP `explode`_ or `str_split`_ (if delimiter is 44 empty) functions for string splitting. 45 46Arguments 47--------- 48 49* ``delimiter``: The delimiter 50* ``limit``: The limit argument 51 52.. _`explode`: https://secure.php.net/explode 53.. _`str_split`: https://secure.php.net/str_split 54