5

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

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

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" #}
  • 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') – Brandon Kelly Jun 14 '14 at 13:35
  • Thanks for this! Helped me out with replacing something with a variable. – mateostabio May 16 at 20:49

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.