I have a variable $var1 = "21,855.00"
. I already tried using preg_replace
and str_replace
but I could not really achieve what I was looking for.
I wish to echo it as 21855
. How can I do that using PHP?
如何在php中将字符串转换为整数
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
5条回答 默认 最新
- dongyong6428 2015-05-08 08:11关注
Just taking note:
If its a literal
string
like:$var1 = '21,855.00'; // string(9) "21,855.00"
You can also do it this way:
$var1 = '21,855.00'; $var2 = filter_var(strtok($var1, '.'), FILTER_SANITIZE_NUMBER_INT); echo $var2; // 21855
Or with regex:
$var1 = '21,855.00'; $var2 = (int) preg_replace('/(?<=\d),(?=\d{3}\b)/','', $var1); var_dump($var2); // int(21855)
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 乌班图ip地址配置及远程SSH
- ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
- ¥15 PSPICE制作一个加法器
- ¥15 javaweb项目无法正常跳转
- ¥15 VMBox虚拟机无法访问
- ¥15 skd显示找不到头文件
- ¥15 机器视觉中图片中长度与真实长度的关系
- ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
- ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
- ¥15 java 的protected权限 ,问题在注释里