7

How do I replace ";" with "," in string "Red; Green; Blue"?

{% set rgb = "Red; Green; Blue" %}

1 Answer 1

21

Twig has a replace filter for that. You can replace strings/vars with strings/vars:

{% set string = "I love apples" %}
{% set var = "like" %}
{% set string = string|replace({'apples': 'kiwis', 'love': var}) %}

{{ string }} {# Prints "I like kiwis" #}

Your example:

{% set rgb = "Red; Green; Blue" %}
{% set rgb = rgb|replace({';': ','}) %}

{{ rgb }} {# Prints "Red, Green, Blue" #}
2
  • 10
    Worth noting that Craft actually comes with its own version of the replace filter, which actually supports a simpler syntax if you’re only replacing one thing: |replace('apples', 'kiwis') Commented Jun 14, 2014 at 13:35
  • Thanks for this! Helped me out with replacing something with a variable. Commented May 16, 2019 at 20:49

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.