Reputation: 4013
Using this Twig template:
{% for line in lines%}
{{line}} after trim('.php') is: {{line|trim('.php')}}<br>
{% endfor %}
Using a controller in Silex to render the above template:
$app->get('/try', function () use ($app)
{
$lines=[];
$lines[]='123.php';
$lines[]='news.php';
$lines[]='map.php';
return $app['twig']->render('try.html.twig', ['lines'=>$lines]);
}
);
I receive the following output:
123.php after trim('.php') is: 123
news.php after trim('.php') is: news
map.php after trim('.php') is: ma
Note the last trim: map.php
should become map
but is now ma
.
Upvotes: 0
Views: 1582
Reputation: 20193
I think this is expected behavior.
trim()
does not trim a substring but list of characters instead.
So:
map.php after trim('.php') is: ma
map.php -> does it start/end with any of ['.php'] -> TRUE -> map.ph
map.ph -> does it start/end with any of ['.php'] -> TRUE -> map.p
map.p -> does it start/end with any of ['.php'] -> TRUE -> map.
map. -> does it start/end with any of ['.php'] -> TRUE -> map
map -> does it start/end with any of ['.php'] -> TRUE -> ma
ma -> does it start/end with any of ['.php'] -> FALSE -> ma
It acts exactly like: php's trim()
Hope this helps.
Upvotes: 4
Reputation: 16502
As per the other listed answers, you're misinterpreting how Twig's trim
function works (which uses the argument as a character map).
What you are probably looking for is the replace
filter instead:
{% for line in lines %}
{{ line }} after replace({'.php': ''}) is: {{ line|replace({'.php': ''}) }}<br>
{% endfor %}
Upvotes: 5
Reputation: 17759
Trim (as with the actual PHP function) uses the argument (or 2nd argument in regular PHP) as a character map rather than a set string.
This argument could be better explained as a list of characters that, if found in any order, will be trimmed from the beginning or end of the given string
.
Upvotes: 2