php – Currency formatting to save to database as DECIMAL

Question:

I'm using a mysql database, the field format is Decimal (7,2) and it receives the salary value. Based on what the user types, I store it in the bank like this:

str_replace(',','.', $_POST['txtSalario'])

Replacing the comma with a period. The problem is that if he types ex: 3.145.89, the database refuses because he understands that the first point is already the decimal place. How to handle this data entry to receive values ​​typed as 3145.89 or 3145.89?
I'm using PHP 5.3 and mysql 5.5

Answer:

In the input field of the edit screen, it is necessary to replace the semicolon, so that when saving the function below it doesn't give an error.

On the edit screen:

$valor = $valor = str_replace('.', ',',$NumValorDeconto);

And to save I used a function to clear.

public static function moeda($get_valor) {

        $source = array('.', ',');
        $replace = array('', '.');
        $valor = str_replace($source, $replace, $get_valor); //remove os pontos e substitui a virgula pelo ponto
        return $valor; //retorna o valor formatado para gravar no banco
    }
Scroll to Top