将逗号分隔的字符串转换为整数PHP?

9
有没有办法使用PHP将“185,345,321”转换为185345321?

通过去掉逗号? - Brian
这是一个好问题。如果你尝试使用(int)或intval将"1,096"转换,结果将会是1。 - stealthysnacks
8个回答

8
是的,这是可能的:
$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

8
这可以通过以下方式实现:-
$intV = intval(str_replace(",","","185,345,321"));

这里使用intval()string转换为integer


5
您可以通过执行以下操作来去除逗号
$newString = str_replace(",", "", $integerString);

那么

$myNewInt = intval($newString);

4

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

3
您可以使用字符串替换,str_replacepreg_replace 都是可行的解决方案。
$string = str_replace(",","","185,345,321");

之后,PHP 应该负责类型转换,因此您将处理一个整数。


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

3

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接