Php 如何将电话号码字符串转换为int或float,结尾不带.0?
数据库中的值是63906XXXXXX,这是一个电话号码。现在,由于我有一个32位版本的PhP,如何精确地提取这个数字12位 以下是我获取字符串的代码: $contactNum=$ngo_cotactNoPhp 如何将电话号码字符串转换为int或float,结尾不带.0?,php,laravel,Php,Laravel,数据库中的值是63906XXXXXX,这是一个电话号码。现在,由于我有一个32位版本的PhP,如何精确地提取这个数字12位 以下是我获取字符串的代码: $contactNum=$ngo_cotactNo $numeric_indexed_array = array_values($contactNum); $n = $numeric_indexed_array[0]; $numeric_indexed_array = array_values($
$numeric_indexed_array = array_values($contactNum);
$n = $numeric_indexed_array[0];
$numeric_indexed_array = array_values($contactNum);
$n = $numeric_indexed_array[0];
$number = (int) $n;
dd($number);
dd($n);
输出:“639063449729”
以下是我将as字符串转换为int的代码:
$contactNum=$ngo_cotactNo
$numeric_indexed_array = array_values($contactNum);
$n = $numeric_indexed_array[0];
$numeric_indexed_array = array_values($contactNum);
$n = $numeric_indexed_array[0];
$number = (int) $n;
dd($number);
输出:2147483647
漂浮的绳子
输出:639063449729.0
我想要的是整个号码639063449729,这样我就可以用它来传递我的短信API。我从昨天开始就一直在做这个,但运气不好。不要将电话号码转换成整数或浮点数。在32位环境中,正整数的最大值为2^31,比2 000多一点。在32位中放置一个12位数字是完全不可能的。也不要使用浮动。浮动精度有限,因此您的电话号码可能会四舍五入到下一个可表示的号码。请把它们放在一根绳子里 维基百科说: 在国际电话网中,电话的格式 数字由ITU-T建议E.164标准化。此代码 指定整个数字应为15位或更短,并且 以国家前缀开头
为什么要将它从字符串转换为Int或float?可能最好将其保留为字符串。它应该保持为字符串。请参见此处的讨论-可能有一些原因说明为什么将电话号码存储为数据类型而不是string@AndrewL-原因是此号码应作为有效电话号码而不是字符串参数传递,因为它用于向此特定用户发送消息数字。@nogad-我正在使用chikka api进行消息传递,唯一的问题是php/laravel的字符串到数字的正确转换。我到处搜索,但仍然没有找到它。问题解决了:我只是在数据库中将数据类型设置为BIGINT。感谢您的回答。我从中学到了一些东西。