3v4l.org

run code in 300+ PHP versions simultaneously
<?php $pattern = "~([^\d.,]|[.,](?!\d{2}$))|(,)~m"; $strings = [ "2.578,20", "2 354,20", "234", "234,256.20", "234,205", "246 548", "244'056", "26'155.25", "12,20", "13.33", ]; foreach ($strings as $string) { echo preg_replace_callback($pattern, function ($matches) { if (isset($matches[2])) return "."; if (isset($matches[1])) return ""; }, $string) . PHP_EOL; }

preferences:
32.9 ms | 404 KiB | 5 Q