gpt4 book ai didi

symfony - PHP basename Twig 等价物

转载 作者:行者123 更新时间:2023-12-01 08:26:32 25 4
gpt4 key购买 nike

Twig 中是否有与 PHP basename() 等效的函数?

就像是:

$file = basename($path, ".php");

最佳答案

使用 Twig,我们可以找到最后一个点 ( . ) 之后的字符串,并将其从字符串中删除以获得文件名(但如果有多个点,它将不起作用)。

{% set string = "file.php" %}
{{ string|replace({ ('.' ~ string|split('.')|last): '' }) }}
{# display "file" #}
{% set string = "file.html.twig" %}
{{ string|replace({ ('.' ~ string|split('.')|last): '' }) }}
{# display "file.html" and not "file" #}

解释:
{{ string|replace({     # declare an array for string replacement
( # open the group (required by Twig to avoid concatenation with ":")
'.'
~ # concatenate
string
|split('.') # split string with the "." character, return an array
|last # get the last item in the array (the file extension)
) # close the group
: '' # replace with "" = remove
}) }}

关于symfony - PHP basename Twig 等价物,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32589506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com