Stevan Tosic
Stevan Tosic

Reputation: 7249

Replace string on wanted position in twig

I have some string which has multiple words and between them there is always ', ' but also on end

str = ['str1, str2, str3, ']

I need to do

str|replace(...)

but with condition to remove only last set of character

', '

All this I need to do in twig not in Controller

Upvotes: 0

Views: 482

Answers (1)

Renan Taranto
Renan Taranto

Reputation: 572

You can use trim:

{% set str = ['foo, bar, foobar, '] %}
{{ str[0]|trim(', ') }}
//Outputs 'foo, bar, foobar'

Upvotes: 1

Related Questions