Twig split the line after the first spesific character as a delimiter

I have a question with the same scenario as this question , except that there can be more than one in the text _.

Example:

57b42a0557cdd_Filename_whatever.pdf

How can I omit everything to the first underscore (including underscore) to save the rest as Filename_whatever.pdf

A random uniquifier may have different lengths, but there will always be an underscore between it and the actual file name.

As in the above question; {{ filename|split('_')[1] }}may work, but what if the actual file name has an underscore?

I want this to be desirable in twig just for showing purposes, because a fully unique name is used in different parts of the project.

+4
2

, split limit explode, do:

{{ '57b42a0557cdd_Filename_whatever.pdf'  | split('_', 2)[1] }}
{{ '57b42a0557cdd_Filename_what_ever.pdf' | split('_', 2)[1] }}
{{ '57b42a0557cdd_File_name_whatever.pdf' | split('_', 2)[1] }}
+7

(, ) , , , .
:

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

twig;

{{ myObject.cleanfilename }}
+1

Source: https://habr.com/ru/post/1651514/


All Articles