How to remove character from a String and transform into integer value with PHP?

1

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?

    
asked by anonymous 27.07.2018 / 22:40

3 answers

4

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 .

    
27.07.2018 / 22:42
2

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 $

    
27.07.2018 / 22:42
0

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));
    
28.07.2018 / 13:39