Mentos93 Mentos93 - 2 months ago 4
PHP Question

Twig split a string after first spesific character as delimiter

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;


How can I omit everything up until the first underscore (including the underscore) to keep the rest like

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.


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] }}