I'm trying to convert the following numbers, so that all have the same value of 5460 if there is no comma or dot used at the third last position or 5460,00 if there is a comma or dot.
Here are my test numbers:
5460
5.460
5.460€
5460,00
5460,00€
5460.00
5460.00€
5.460,00
5.460,00€
5,460.00
5,460.00€
I used the following regex with preg_replace:
preg_replace('/[^0-9\,\-]+/','',$number);
The result was the following
5460 -> 5460
5.460 -> 5460
5.460€ -> 5460
5460,00 -> 5460,00
5460,00€ -> 5460,00
5460.00 -> 546000 // wrong
5460.00€ -> 546000 // wrong
5.460,00 -> 5460,00
5.460,00€ -> 5460,00
I don't know how to optimize the regex, so that also the wrong values will be correct replaced like this:
5460.00 -> 546000 // wrong because should be 5460,00
5460.00€ -> 546000 // wrong because should be 5460,00
Test case:
$numbers = array('5460', '5.460', '5.460€', '5460,00', '5460,00€', '5460.00', '5460.00€', '5.460,00', '5.460,00€');
foreach ($numbers as $number)
echo $number." -> ".preg_replace('/[^0-9\,\-]+/','',$number) . "
";
So I don't know how to check if the last two digits have a dot before, and if yes to replace it with a comma. But only for the last two digits.
Thanks