Twig "trim" 未按预期工作
Twig "trim" not working as expected
使用这个 Twig 模板:
{% for line in lines%}
{{line}} after trim('.php') is: {{line|trim('.php')}}<br>
{% endfor %}
使用 Silex 中的控制器呈现上述模板:
$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]);
}
);
我收到以下输出:
123.php after trim('.php') is: 123
news.php after trim('.php') is: news
map.php after trim('.php') is: ma
注意最后一个 trim:map.php
应该变成 map
但现在是 ma
。
我认为这是预期的行为。
trim()
不是 trim 子字符串而是字符列表。
所以:
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
它的行为完全像:php's trim()
希望对您有所帮助。
Trim(与实际的 PHP 函数一样)使用参数(或常规 PHP 中的第二个参数)作为字符映射而不是集合字符串。
这个论点可以更好地解释为 a list of characters that, if found in any order, will be trimmed from the beginning or end of the given string
。
根据其他列出的答案,您误解了 Twig 的 trim
函数的工作原理(它使用参数作为字符映射)。
您可能正在寻找的是 replace
过滤器:
{% for line in lines %}
{{ line }} after replace({'.php': ''}) is: {{ line|replace({'.php': ''}) }}<br>
{% endfor %}
使用这个 Twig 模板:
{% for line in lines%}
{{line}} after trim('.php') is: {{line|trim('.php')}}<br>
{% endfor %}
使用 Silex 中的控制器呈现上述模板:
$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]);
}
);
我收到以下输出:
123.php after trim('.php') is: 123
news.php after trim('.php') is: news
map.php after trim('.php') is: ma
注意最后一个 trim:map.php
应该变成 map
但现在是 ma
。
我认为这是预期的行为。
trim()
不是 trim 子字符串而是字符列表。
所以:
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
它的行为完全像:php's trim()
希望对您有所帮助。
Trim(与实际的 PHP 函数一样)使用参数(或常规 PHP 中的第二个参数)作为字符映射而不是集合字符串。
这个论点可以更好地解释为 a list of characters that, if found in any order, will be trimmed from the beginning or end of the given string
。
根据其他列出的答案,您误解了 Twig 的 trim
函数的工作原理(它使用参数作为字符映射)。
您可能正在寻找的是 replace
过滤器:
{% for line in lines %}
{{ line }} after replace({'.php': ''}) is: {{ line|replace({'.php': ''}) }}<br>
{% endfor %}