]> git.immae.eu Git - github/wallabag/wallabag.git/blob - vendor/twig/twig/doc/filters/split.rst
twig implementation
[github/wallabag/wallabag.git] / vendor / twig / twig / doc / filters / split.rst
1 ``split``
2 =========
3
4 .. versionadded:: 1.10.3
5 The split filter was added in Twig 1.10.3.
6
7 The ``split`` filter splits a string by the given delimiter and returns a list
8 of strings:
9
10 .. code-block:: jinja
11
12 {{ "one,two,three"|split(',') }}
13 {# returns ['one', 'two', 'three'] #}
14
15 You 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 {{ "one,two,three,four,five"|split(',', 3) }}
28 {# returns ['one', 'two', 'three,four,five'] #}
29
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).
32
33 .. code-block:: jinja
34
35 {{ "123"|split('') }}
36 {# returns ['1', '2', '3'] #}
37
38 {{ "aabbcc"|split('', 2) }}
39 {# returns ['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
46 Arguments
47 ---------
48
49 * ``delimiter``: The delimiter
50 * ``limit``: The limit argument
51
52 .. _`explode`: http://php.net/explode
53 .. _`str_split`: http://php.net/str_split