9

Is there any way you can convert "185,345,321" to 185345321 by using PHP?

Cœur
  • 37,241
  • 25
  • 195
  • 267
user2562125
  • 179
  • 1
  • 2
  • 10

8 Answers8

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

4
$string= "185,345,321";
echo str_replace(",","",$string);
Moeed Farooqui
  • 3,604
  • 1
  • 18
  • 23
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
3
$str = "185,345,321";
$newstr = str_replace(',','',$str);
echo $newstr;
waqas
  • 181
  • 2
  • 7
  • 15
3
str_replace(",","","185,345,321")

str_replace tutorial

Ryan decosta
  • 455
  • 1
  • 4
  • 10