Is there any way you can convert "185,345,321" to 185345321 by using PHP?
Asked
Active
Viewed 1.6k times
9
-
By removing the commas? – Brian Oct 15 '13 at 19:26
-
This is a good question. If you try to convert "1,096" using (int) or intval, the result will be 1. – stealthysnacks Oct 12 '16 at 20:45
8 Answers
8
Yes, it's possible:
$str = "185,345,321";
$newStr = str_replace(',', '', $str); // If you want it to be "185345321"
$num = intval($newStr); // If you want it to be a number 185345321

Salim Djerbouh
- 10,719
- 6
- 29
- 61

Misha Zaslavsky
- 8,414
- 11
- 70
- 116
8
This can be achieved by-
$intV = intval(str_replace(",","","185,345,321"));
Here intval()
is used to convert string
to integer
.

Misha Zaslavsky
- 8,414
- 11
- 70
- 116

ritesh
- 2,245
- 2
- 25
- 37
5
You can get rid of the commas by doing
$newString = str_replace(",", "", $integerString);
then
$myNewInt = intval($newString);

dckuehn
- 2,427
- 3
- 27
- 37
4
Yes, use str_replace()
Example:
str_replace( ",", "", "123,456,789");
Live example: http://ideone.com/Q7IAIN

Marco Pietro Cirillo
- 864
- 2
- 8
- 26
3
You can use string replacement, str_replace
or preg_replace
are viable solutions.
$string = str_replace(",","","185,345,321");
PHP should take care of type casting after that so you deal with an integer.

Crackertastic
- 4,958
- 2
- 30
- 37