Search code examples
typo3fluidtypo3-8.xviewhelper

How to remove strings at left of "-" in filename in TYPO3 Fluid?


Using TYPO3 8 LTS, we got many standardized filenames like:

ABC_105-Report.pdf 
DEFGH_110-Brochure.ppt

We need to remove whatever is at left of "-" so it becomes a list like this in TYPO3 Frontend:

Report.pdf 
Brochure.ppt

We are already using VHS Viewhelpers which contains Format:Eliminiate, Substring so it may be part of the solution.


Solution

  • One possible solution is VHS: Format / PregReplaceViewHelper.

    <f:alias map="{filenames: {
        0: 'ABC_105-Report.pdf',
        1: 'DEFGH_110-Brochure.ppt',
        2: 'FilenameWithoutMagicChar.jpg',
        3: 'Multiple-Magic-Chars.jpg'}}">
        <ul>
            <f:for each="{filenames}" as="filename">
                <li>
                    {v:format.pregReplace(
                        subject: filename, 
                        pattern: '/^[^-]*-/', 
                        replacement: ''
                    )}
                </li>
            </f:for>
        </ul>
    </f:alias>
    

    Result:

    • Report.pdf
    • Brochure.ppt
    • FilenameWithoutMagicChar.jpg
    • Magic-Chars.jpg

    If 'Chars.jpg' is required instead of 'Magic-Chars.jpg', the regular expression is /-.*/.