Search code examples
regexreplacesmarty

Smarty regex replace Ordernumber


I have an Ordernumber that looks like this: 1001-00001.2 but I want it without the extra .2

I tried to use the following in my code:

{$sArticle.ordernumber|regex_replace:"/'.'/\d":" "}

it didn't work because I don't know how I can use the dot.


Solution

  • Dot matches any single character. Try escaping it:

    {$sArticle.ordernumber|regex_replace:"/\.\d+$/":""}
    

    There's actually a few changes, here.

    • Everything was moved to be within the /.../ that mark a regex
    • The dot was escaped with \.
    • A quantifier (+) was added to \d so it matches one or more digits.
    • An anchor ($) is used to make sure it doesn't match anywhere but the end of the string.
    • The replacement is now an empty string