将数字转换为国际数字,PHP
在PHP中,将手机号码转换为国际格式的最快方法是什么:
所以07123456789变成447123456789。
我尝试了几种方法,但似乎无法让它工作。
这是当前脚本:
if(strlen($gsm) > 2) {
if(!substr_compare($gsm, "07", 0, 2, false)) {
unset($gsm);
}
elseif (substr_compare($gsm, "07", 0, 3, true)) {
if(strlen($gsm) == 11) {
return "447" . substr($gsm, 2);
}
}
}
注意:此脚本仅在数字与正则表达式匹配时运行。
In PHP what is the quickest way to convert an mobile number to international format:
So 07123456789 becomes 447123456789.
I have tried a few ways and cant seem to get it to work.
This is current script:
if(strlen($gsm) > 2) {
if(!substr_compare($gsm, "07", 0, 2, false)) {
unset($gsm);
}
elseif (substr_compare($gsm, "07", 0, 3, true)) {
if(strlen($gsm) == 11) {
return "447" . substr($gsm, 2);
}
}
}
Note: This script only runs if the number matches a regex.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许是这样的?
或者如果您特别只想使用英国手机号码:(
注意:我假设英国特定,因为您在问题中指定了“44”)
这确实使用正则表达式,但执行速度应该相当快,因为它锚定到字符串的开头。
Something like this, perhaps?
or if you specifically only want to do UK mobile numbers:
(note: I'm assuming UK-specific since you specified '44' in the question)
This does use Regex, but should be pretty quick in execution speed since it is anchored to the begining of the string.
我想到的最快的方法是,只要你知道此时的数字与格式 07xxxxxx 匹配:
Fastest way that comes to mind for me, as long as you are shre the number matches the format 07xxxxxx at this point:
有一个 PEAR 包用于验证国际电话数字。
There is a PEAR package for validating international telephone numbers.