split

The split filter splits a string by the given delimiter and returns a list of strings:

分割フィルターは、指定された区切り文字で文字列を分割し、文字列のリストを返します。
1
2
{% set foo = "one,two,three"|split(',') %}
{# foo contains ['one', 'two', 'three'] #}

You can also pass a limit argument:

limit 引数を渡すこともできます:
  • If limit is positive, the returned array will contain a maximum of limit elements with the last element containing the rest of string;
    limit が正の場合、返される配列には最大 oflimit 要素が含まれ、最後の要素には残りの文字列が含まれます。
  • If limit is negative, all components except the last -limit are returned;
    limit が負の場合、最後の -limit を除くすべてのコンポーネントが返されます。
  • If limit is zero, then this is treated as 1.
    limit がゼロの場合、これは 1 として扱われます。
1
2
{% 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).

区切り文字が空の文字列の場合、値は equalchunks で分割されます。長さは limit 引数によって設定されます (デフォルトでは 1 文字)。
1
2
3
4
5
{% 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.

内部的には、Twig は文字列の分割に PHP の爆発または str_split (区切り文字が空の場合) 関数を使用します。

Arguments

  • delimiter: The delimiter
    delimiter: デリミタ
  • limit: The limit argument
    limit: 制限引数