How can I split (or explode) a Twig string like this:

{% set fruits = "Apples; Bananas; Tomatoes" %}

into an array like this:

{{ ["Apples", "Bananas", "Tomatoes"] }}

You would use the split filter.

{% set fruits = "Apples; Bananas; Tomatoes" | split('; ') %}
  • Strange why it is shown that my question was viewed only 1 Time... – nicael Jun 14 '14 at 18:31
  • Of course fruits is MUCH more better and accurately then items. I had some doubts but now in 5 mins I'll accept your answer. – nicael Jun 14 '14 at 18:36

Use Twig's split filter.

{{ "Apples; Bananas; Tomatoes"|split('; ') }}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.