1
votes

I want to trim a string using twig. The documentation for trim is located here.

{{ 'I like Twig!'|trim('!') }}

{# outputs 'I like Twig' #}

The above example trims exclamation marks from the string.

Consider the following:

{{ 'ROLE_USER'|trim('ROLE_') }}

One would think this would trim ROLE_ and return USER. That's not how it works:

{# outputs 'US' #}

This is because the letters E and R are also in ROLE_, hence they are also removed.

How can I circumvent this, perhaps with a regular expression, or replacing only exactly the string I want?

1

1 Answers

5
votes

What about the replace filter?

{{ 'ROLE_USER'|replace({'ROLE_': ''}) }}
//outputs
USER