2

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

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

into an array like this:

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

2 Answers 2

5

You would use the split filter.

{% set fruits = "Apples; Bananas; Tomatoes" | split('; ') %}
2
  • Strange why it is shown that my question was viewed only 1 Time...
    – nicael
    Commented Jun 14, 2014 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
    Commented Jun 14, 2014 at 18:36
1

Use Twig's split filter.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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