I have a value that comes from the form this way:
R$ 1.500,95
I need it to look like this:
R$ 1500,95
How to make it become like the 2nd way? I need it to look exactly the way it was quoted.
I have a value that comes from the form this way:
R$ 1.500,95
I need it to look like this:
R$ 1500,95
How to make it become like the 2nd way? I need it to look exactly the way it was quoted.
Remove the point and the R $.
<?php
$moeda = "R$ 1.500,95";
// Com str_replace (Remove a string 'R$ ' e '.'
$valor = str_replace( array('R$ ', '.'), '', $moeda);
echo $valor; //1550,95
// Com preg_replace (Remove tudo que for diferente de dígito e vírgula)
$valor = preg_replace( '/([^\d,])/', '', $moeda);
$valor = preg_replace('/[,]+/', ',', $valor); // Fix para caso seja passado mais de 1 ","
echo $valor; //1550,95
?>
Another solution would be to remove only keep the integers and divide them by 100.
$moeda = 'R$ 1.550,52';
$valor = preg_replace('/[^0-9]/', '', $moeda);
$valor = bcdiv($valor, 100, 2);
$valor = strtr($valor, '.', ',');
echo $valor;
Result:
1550.52
In this way, this would become:
string : 0e100 => 1,00
double : 0e100 => 0,00
string : 0x98 => 0,98
integer : 0x98 => 1,52
string : R$ 100.00 => 100,00
integer : 10000 => 100,00
double : 100.00 => 1,00
integer : 12345 => 123,45
string : 123.45 => 123,45
double : 123.45 => 123,45
string : 1a2b3c4d5e => 123,45
string : 123,45 => 123,45
string : R$ 123,45 => 123,45
string : 1 => 0,01
integer : 1 => 0,01
double : 9.0E+99 => 8999999999999999948859130765266355329578537025198862586562510896759102769772101980841694466750283776,00
integer : 9223372036854775807 => 92233720368547758,07
This function, as it is, has " problems " with float , I do not recommend using float / double. The float of 100.00
will be 1,00
and 100.01
will become 100,01
, ie the 100.00
will not go to 100,00
, which may not be expected.
Obviously, impersonation must contain the cents , if it is just R$ 1.550
will move to 15,50
. However, informing 1.550,00
will not have problem, as will 155000
or even 1a550b00
.
You can use the function preg_replace
or str_replace
, see below with preg_replace
:
echo preg_replace('/^[0-9,]/', '', $valor);
// Saída: 1500,95