Best way to convert string[which is basically a comma seprated number] to an integer in PHP

Viewed 134

I have a value like 12,25,246 I want to get 1225246 (must be an integer)

How can I do that?

I have searched a lot but there are direct answer like converting string to integer but not like this actually these both are like integer

I have tried php formate_number but it did not worked.

2 Answers

You could use a combination of intval() and str_replace() to do this.

Example:

$value = '12,25,246';
var_dump(intval(str_replace(',','',$value)));
// Yields: "int(1225246)"

Sandbox

$number = (int)str_replace(',', '', '12,25,246');

here is it

Related