Search code examples
phpparsingfloating-pointcurrency

Unformat money when parsing in PHP


Is there a way to get the float value of a string like this: 75,25 €, other than parsefloat(str_replace(',', '.', $var))?

I want this to be dependent on the current site language, and sometimes the comma could be replaced by dot.


Solution

  • You can use

    Example from Manual:

    $formatter = new NumberFormatter('de_DE', NumberFormatter::CURRENCY);
    var_dump($formatter->parseCurrency("75,25 €", $curr));
    

    gives: float(75.25)

    Note that the intl extension is not enabled by default. Please refer to the Installation Instructions.