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.pdf
How 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.
If limit is zero, then this is treated as 1. If the delimiter is an empty string, then value will be split by equal chunks. Length is set by the limit argument (one character by default). Internally, Twig uses the PHP explode or str_split (if delimiter is empty) functions for string splitting.
If not specified, the default value is 1. Here we are splitting the first name and last name from the string using STRTOK function. The delimiter character is specified as space and it returns the first name as we mentioned the token number as 1.
It returns the particular string token based on the tokennum argument. Input_String : A character string or String expression Delimiter : A delimiter character. Each character in the string is considered as delimiter.If not specified, the default delimiter character is space.
How to split the string based on delimiter in Teradata. STRTOK function in Teradata. STRTOK function is used to split the string into tokens based on the specified delimiter. It returns the particular string token based on the tokennum argument.
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] }}
Another option (assuming that the file is part of an entity) is to write a function on the entity that that returns what you want.
For example;
<?php
namespace AppBundle\Entity;
class MyEntity
{
// ... other attributes
private $hashFileName;
private $cleanFileName;
// other functions
public function getHashFileName()
{
// as per you example; 57b42a0557cdd_Filename_whatever.pdf
return $this->hashFileName;
}
public function getCleanFileName()
{
$withouthash = explode('_', $this->hashFileName,2);
return $withouthash[1];
}
}
Then in your twig file;
{{ myObject.cleanfilename }}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With