TWIG: trim two characters from a string

6.4k Views Asked by At

I'm very new to TWIG.

I have a string ₹1,324, or ₹324 compare it with integer value 3000, so I want to trim characters and ,

I know how to trim one character.

{% if foo |trim('₹')  |number_format > 3000 %}

help me to how to do it.

thanks in advance.

2

There are 2 best solutions below

0
On

Use php str_replace

str_replace([",", "₹"], "", $str);

replace for twig

{% if foo |replace({'₹':'', ',':''}) |number_format > 3000 %}

https://twig.symfony.com/doc/2.x/filters/replace.html

So how you would ideally do it would be pass in just a regular integer to your twig template, do the comparison & then if you need to display the whole string on the page, prefix the number_format to get the comma and if you wish decimal places.

https://twig.symfony.com/doc/2.x/filters/number_format.html

0
On

As mentioned in comments, this is not a job for a view. But the replace filter can do this:

{% if foo |replace({'₹':'', ',':''}) > 3000 %}

You don't want to use the number_format filter as this will just reintroduce punctuation.