我对这个问题的相同场景有一个疑问,除了文本中可能会出现多个_ 。
一个例子;
57b42a0557cdd_Filename_whatever.pdf我怎么能省略所有内容,直到第一个下划线(包括下划线)来保持其余的像Filename_whatever.pdf
随机唯一标识符可以具有不同的长度,但是它和实际文件名之间总是会有一个下划线。
就像在提到的问题中一样; {{ filename|split('_')[1] }}可能有效,但如果实际文件名有下划线怎么办?
我希望它最好是用于显示目的的树枝,因为完整的唯一名称也用于项目的不同部分。
I have a question with the same scenario as this question, except that it might happen that more than one _ is in the text.
An example;
57b42a0557cdd_Filename_whatever.pdfHow can I omit everything up until the first underscore (including the underscore) to keep the rest like Filename_whatever.pdf
The random uniquifier can be of a different length, but there will always be an underscore between it and the actual filename.
Like in the mentioned question; {{ filename|split('_')[1] }} might work, but what if the actual filename has an underscore?
I want it preferably in twig just for displaying purposes because the complete unique name is used on different parts of the project as well.
最满意答案
从文档中可以看出, split还支持limit参数为explode ,因此您可以这样做 :
{{ '57b42a0557cdd_Filename_whatever.pdf' | split('_', 2)[1] }} {{ '57b42a0557cdd_Filename_what_ever.pdf' | split('_', 2)[1] }} {{ '57b42a0557cdd_File_name_whatever.pdf' | split('_', 2)[1] }}As seen in the documentation, split also supports the limit parameter as explode, so you can do :
{{ '57b42a0557cdd_Filename_whatever.pdf' | split('_', 2)[1] }} {{ '57b42a0557cdd_Filename_what_ever.pdf' | split('_', 2)[1] }} {{ '57b42a0557cdd_File_name_whatever.pdf' | split('_', 2)[1] }}更多推荐
发布评论