Either I googled wrong or I'm the first asking for this actually really simple functionality.
I have strings like "18.439", "500", "20.000" that need to be converted into integers: 18439, 500, 20000 (where I live, we use points as thousand seperators).
I was hoping for intval() to handle this, as in C# you can add a second parameter NumberStyles.AllowThousands which perfectly works.
You can use str_replace in PHP . like this.
intval(str_replace('.','',$string))
Assuming you are dealing only with integers.
You can use the str_replace
function for this:
$a = '18.439';
$a = (float) str_replace(['.', ','], ['', '.'], $a);
var_dump($a);
It will replace every .
in your string to ''
(empty string) and every ,
to .
, and then will convert the result to float.
If you need to result as int
you can use:
$a = '18.439';
$a = (int) str_replace(['.', ','], ['', '.'], $a);
var_dump($a);