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

时间:2013-10-15 19:24:25

标签: php string int

有什么方法可以使用PHP将“185,345,321”转换为185345321吗?

8 个答案:

答案 0 :(得分:6)

是的,可能:

$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

答案 1 :(得分:4)

这可以通过 -

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

此处intval()用于将字符串转换为整数。

答案 2 :(得分:2)

是的,请使用str_replace()

示例:

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

实例: http://ideone.com/Q7IAIN

答案 3 :(得分:2)

你可以通过

删除逗号
$newString = str_replace(",", "", $integerString);

然后

$myNewInt = intval($newString);

答案 4 :(得分:2)

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

答案 5 :(得分:1)

您可以使用字符串替换,str_replacepreg_replace是可行的解决方案。

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

PHP 在此之后处理类型转换,以便处理整数。

答案 6 :(得分:1)

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

答案 7 :(得分:1)

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

str_replace tutorial