php字符串到int [英] php string to int

查看:102
本文介绍了php字符串到int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$a = '88';
$b = '88 8888';

echo (int)$a;
echo (int)$b;

如预期的那样,都产生88.任何人都知道是否有一个字符串到int函数将适用于$ b的价值并生产888888?我已经google了一下,没有运气。

as expected, both produce 88. Anyone know if there's a string to int function that will work for $b's value and produce 888888? I've googled around a bit with no luck.

谢谢

推荐答案

你可以在转换之前删除空格 int

You can remove the spaces before casting to int:

(int)str_replace(' ', '', $b);

另外,如果你想剥去其他常用的数字分隔符(例如),您可以为函数提供一个数组(请注意 - 在某些国家/地区,例如我的逗号用于分数表示法):

Also, if you want to strip other commonly used digit delimiters (such as ,), you can give the function an array (beware though -- in some countries, like mine for example, the comma is used for fraction notation):

(int)str_replace(array(' ', ','), '', $b);

这篇关于php字符串到int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆