How to simplify the use of str_replace

1

As I explained in this question , I am using maskMoney to generate a currency mask in multiple fields input of type="text" , except that when I perform arithmetic operations with the values obtained, and then I use number_format in the output, it gives several types of formatting problems (does not include comma, etc.).

The solution I found on that topic was as follows:

$ultsalbase = $_POST["Tultsal"]; 

$ultsalbase2 = str_replace('.', '', $ultsalbase); 
$ultsalbase3 = str_replace(',', '.', $ultsalbase2);

In this way, when the value enters the form in the format 1.000,00, the str_replace transforms it to 1000.00. Then after doing the calculations, number_format correctly returns the values in currency (1,000.00).

What happens is that because there are many fields, I'm thinking it might be wrong to have to create two new variables for each field, just to change the formatting.

Is not there a simpler solution for this?

I tried this:

$ultsalbase = $_POST["Tultsal"] . str_replace('.', '', $ultsalbase) . str_replace(',', '.', $ultsalbase); 

But it did not work. Any ideas?

    
asked by anonymous 21.04.2015 / 19:46

1 answer

1

Create a function to apply to all required items of $_POST , for example:

function formatFromMaskMoney($value) {
    $value = str_replace(' ', '', $value);
    $value = str_replace('.', '', $value);
    $value = str_replace(',', '.', $value);
    return (double) $value;
}

$_POST["variavel-1"] = formatFromMaskMoney($_POST["variavel-1"]);
$_POST["variavel-2"] = formatFromMaskMoney($_POST["variavel-2"]);
$_POST["variavel-3"] = formatFromMaskMoney($_POST["variavel-3"]);
$_POST["variavel-4"] = formatFromMaskMoney($_POST["variavel-4"]);

//exemplo:
echo number_format(formatFromMaskMoney('4.233.456.700, 99') / formatFromMaskMoney('1.000.000,00'), 2, ",", ".");

Or if you want to make it easier you can apply array and use array_map , such as:

function formatFromMaskMoney($key) {
    if (isset($key) && isset($_POST[$key])) {
        $_POST[$key] = str_replace(' ', '', $_POST[$key]);
        $_POST[$key] = str_replace('.', '', $_POST[$key]);
        $_POST[$key] = (double) str_replace(',', '.', $_POST[$key]);
    }

    return $key;
}

$posts = array('variavel-1', 'variavel-2', 'variavel-3', 'variavel-4');
array_map('formatFromMaskMoney', $posts);

echo number_format($_POST['variavel-1'] / $_POST['variavel-2'], 2, ",", ".");

And you can also make the function check if the field is a money value and apply str_replace :

function formatFromMaskMoney($value) {
    if (preg_match('/^[0-9.]+[,]( |)\d{2}$/', $value) !== 0) {
        $value = str_replace(' ', '', $value);
        $value = str_replace('.', '', $value);
        $value = (double) str_replace(',', '.', $value);
    }

    return $value;
}

$_POST = array_map('formatFromMaskMoney', $_POST);
print_r($_POST);//Para verificar os dados antes de usa-los.

echo number_format($_POST['variavel-1'] / $_POST['variavel-2'], 2, ",", ".");

Example online at ideone: link

    
21.04.2015 / 20:16