3v4l.org

run code in 300+ PHP versions simultaneously
<?php class MoneyHelper { public function getAmount($money) { $cleanString = preg_replace('/[^\d.,-]/', '', $money); $onlyNumbersString = preg_replace('/[^\d-]/', '', $money); $separatorsCountToBeErased = strlen($cleanString) - strlen($onlyNumbersString) - 1; $stringWithCommaOrDot = preg_replace('/[,.]/', '', $cleanString, $separatorsCountToBeErased); $removedThousandSeparator = preg_replace('/[.,](?=\d{3,}$)/', '', $stringWithCommaOrDot); //return (float) str_replace(',', '.', $removedThousandSeparator); return [ 'cleanString' => $cleanString, 'onlyNumbersString' => $onlyNumbersString, 'separatorsCountToBeErased' => $separatorsCountToBeErased, 'stringWithCommaOrDot' => $stringWithCommaOrDot, 'removedThousandSeparator' => $removedThousandSeparator, 'result' => (float) str_replace(',', '.', $removedThousandSeparator) ]; } } $obj = new MoneyHelper; echo var_dump($obj->getAmount('-200.000,54')) ;

preferences:
45.48 ms | 402 KiB | 5 Q