I want to find substring of the string or check if there is no such substring using Twig. On the words, I need analogue of 'strstr' or 'strpos' in php. I googled and searched this issue in stackoverflow but nothing found. Does someone know how to solve this problem?
1 Answer
Just searched for the docs, and found this:
Containment Operator: The in operator performs containment test. It returns true if the left operand is contained in the right:
{# returns true #}
{{ 1 in [1, 2, 3] }}
{{ 'cd' in 'abcde' }}
-
3
-
8
-
7Great :) I used it to figure out the current route: <li class="{% if 'gew_team_default_' in app.request.get('_route') %}active{% endif %}"> Aug 28, 2014 at 14:11
-
1Is it affected by slashes or special characters? I am trying to do is find
/some-link
insome-doma.in/some-link
... Sep 7, 2017 at 16:57 -
17Just for completeness, the syntax for testing whether a substring is not present is: {% if 'needle' not in haystack %}– geekbritApr 17, 2018 at 13:00
Find substring in the string in TWIG
, I get a first hit that looks really good. I don't think it's going to get any better than that.