Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twig split a string after first specific character as delimiter

Tags:

php

twig

symfony

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.

like image 802
Mentos93 Avatar asked Aug 17 '16 09:08

Mentos93


People also ask

How do I split a string in twig?

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.

How to split the first and last name from a string?

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.

What is the difference between input_string and delimiter in Python?

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?

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.


2 Answers

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] }}
like image 79
DarkBee Avatar answered Oct 08 '22 09:10

DarkBee


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 }}
like image 41
Rooneyl Avatar answered Oct 08 '22 08:10

Rooneyl