9

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

2
  • By removing the commas? Commented Oct 15, 2013 at 19:26
  • This is a good question. If you try to convert "1,096" using (int) or intval, the result will be 1. Commented Oct 12, 2016 at 20:45

8 Answers 8

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
Sign up to request clarification or add additional context in comments.

Comments

8

This can be achieved by-

$intV = intval(str_replace(",","","185,345,321"));

Here intval() is used to convert string to integer.

Comments

5

You can get rid of the commas by doing

$newString = str_replace(",", "", $integerString);

then

$myNewInt = intval($newString);

Comments

4

Yes, use str_replace()

Example:

str_replace( ",", "", "123,456,789");

Live example: http://ideone.com/Q7IAIN

Comments

4
$string= "185,345,321";
echo str_replace(",","",$string);

Comments

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.

Comments

3
$str = "185,345,321";
$newstr = str_replace(',','',$str);
echo $newstr;

Comments

3
str_replace(",","","185,345,321")

str_replace tutorial

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.