如何从PHP中的印度移动号码中删除国家代码?

如何从PHP中的印度移动号码中删除国家代码?

问题描述:

我正在通过API响应接收带有国家代码(12位数字)的手机号码。但我需要没有国家代码的手机号码(10位数字)。如何从PHP中的印度移动号码中删除国家代码?

我收到什么:

919999999999 

我需要什么:

9999999999 

我曾尝试:

$mobile = "919999999999"; 
$split = preg_split("/[\]+/", $mobile); 
$newmobile = $split[2].$split[3].$split[4].$split[5].$split[6].$split[7].$split[8].$split[9].$split[10].$split[11].$split[12]; 

我知道这是不对的。请帮忙!

+0

看看HTTP ://php.net/manual/en/function.substr.php – ArtOsi

+0

你的总是用国家代码接收它们,或者它们都可以吗? – Andreas

+0

使用substr()函数 – aswindev

这里,将检查移动长度为12个和第2个字符包含一个版本国家代码,然后尝试删除它们。这可以是有用的情况下数字的输入可能会略有不同:

$mobile = "919999999999"; 
if (strlen($mobile) == 12 && substr($mobile, 0, 2) == "91") 
    $mobile = substr($mobile, 2, 10); 
echo $mobile; 
+0

这就是我正在搜索!谢谢! –

只需使用substr()http://php.net/manual/en/function.substr.php

$string = '919999999999'; 
$string = substr($string, 2); 
echo $string; 

将输出9999999999

看它这里https://3v4l.org/T2rHD

由于问题是标签的正则表达式我想你想正则表达式。
这将得到一个数字的最后10位数字。
这意味着它将与国家代码和无国家代码一起使用。

$phone = "919999999999"; 

Preg_match("/\d*(\d{10})/", $phone, $match); 

Echo $match[1]; 

https://3v4l.org/qpTZ9

你可以使用substr

$phoneNumber = '919999999999'; 
$phoneNumberNoCode = substr($phoneNumber, 2); 

如果您正在处理与其他国家的电话号码,你可以使用preg_replace

$countryCode = '91'; //Change here to match country code 
$phoneNumber = '919199191999'; 
$phoneNumberNoCode = preg_replace('/'.$countryCode.'/', '', $phoneNumber, 1); 
+1

你的str_replace会表现得很差,用'911234912911' https://3v4l.org/WrIMF – Andreas

+0

opps谢谢指出。我用preg_replace替换了str_replace,并包含了替换限制参数 –