function formatFromMaskMoney($value) {
$value = str_replace(' ', '', $value);
$value = str_replace('.', '', $value);
$value = str_replace(',', '.', $value);
$value =return number_format($value, 2, ',', '.'double);
return $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, ",", ".");
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]);
$_POST[$key] = number_format($_POST[$key], 2, ',', '.');
}
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, ",", ".");
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);
$value = number_format($value, 2, ',', '.');
}
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, ",", ".");