Twig / PHP - Format string using Replace or Regex

前端 未结 3 1096
忘了有多久
忘了有多久 2021-01-18 04:30

How can I format a string in Twig as follows:

For example: img = 05myphoto-Car.jpg

I need to remove the numeric prefix and -

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-18 04:54

    Better late than never...

    Just register it (for me was on index.php)

    $app['twig']->addFilter('preg_replace', new Twig_Filter_Function(function ($subject, $pattern, $replacement) {
        return preg_replace($pattern, $replacement, $subject);
    }));
    

    Then on the view: {{myVar|preg_replace('/\\d+/','')}}

    Notice that all backslashes MUST be escaped, if not, they will be removed by Twig...

提交回复
热议问题