Search code examples
regexprestashopsmarty

Prestashop I can't make regex_replace works


I have this regex_replace in my template :

{$product.unit_price_full|regex_replace:"/^[\d\s]*,?\d{2}\s*[€$₪]*\s*/u":""}

the $product.unit_price_full returns me "1,12 $ L" and my regex_replace "/^[\d\s]*,?\d{2}\s*[€$₪]*\s*/" : " " is supposed to remove everything before the "L" but it still display everything like the regex didn't worked, but I try it on regex101 it works perfectly.

If I add the unicode markup, it just removes the dollar symbol but nothing else

This is the output I get 1,00  2.21

So I have to capture the space markup in my regex rule?


Solution

  • I suggest the following changes:

    • $ inside a double quoted string literal is used to interpolate string literals and should be escaped with a single backslash if you need to make it a literal $ char. Else, and it is recommended, just use single quotes
    • As you have Unicode chars out of ASCII range in the pattern pass u modifier to the regex so that the string pattern and the input string could be correctly parsed with PCRE.

    Use

    {$product.unit_price_full|regex_replace:'/^[\d\s]*,?\d{2}\s*[€$₪]*\s*/u':""}