I need to convert a string to integer and remove the "R $" from the value.
$valor = $_SESSION['valor'];
echo $valor;
Return me to string: $ 25.00
I need you to remove the $ and return the integer value: 2500.
How can I do this?
I need to convert a string to integer and remove the "R $" from the value.
$valor = $_SESSION['valor'];
echo $valor;
Return me to string: $ 25.00
I need you to remove the $ and return the integer value: 2500.
How can I do this?
If you are not calling decimals, commas, and you want to extract only numbers from the text, do the following:
$str = $_SESSION['valor'];
preg_match_all('!\d+!', $str, $matches);
echo $matches;
The output will be 2500
.
You can simply remove the comma and R $, like this:
$sua_variavel = str_replace ( ',' , '' , $variave_inicial );
$sua_variavel = str_replace ( 'R$' , '' , $sua_variavel );
In the first line will remove the, and in the second the R $
Several ways to achieve this result, another one is:
$valor = preg_replace("/[^0-9]+/i","",$valor);
But it is a numeric output, to convert to int
, use:
$valor = (int) preg_replace("/[^0-9]+/i","",$valor);
//ou
$valor = intval(preg_replace("/[^0-9]+/i","",$valor));