I'm receiving mobile numbers with country code (in 12 digits) from a API response. But I need the mobile number without the country code (in 10 digits).
What I'm receiving:
919999999999
What I need:
9999999999
What I have tried:
$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];
I know this is wrong. Please help!
Here's a version that will check that the mobile length is 12 and the first 2 characters contain the country code, before trying to remove them. This can be useful in case the input of the number can vary a bit:
$mobile = "919999999999";
if (strlen($mobile) == 12 && substr($mobile, 0, 2) == "91")
$mobile = substr($mobile, 2, 10);
echo $mobile;
Just use substr()
http://php.net/manual/en/function.substr.php
$string = '919999999999';
$string = substr($string, 2);
echo $string;
Which will output 9999999999
See it here https://3v4l.org/T2rHD
Since the question is tagged regex I guess you want regex.
This will get the last 10 digits of a number.
This means it will work with both with country code and without.
$phone = "919999999999";
Preg_match("/\d*(\d{10})/", $phone, $match);
Echo $match[1];
You could use substr
:
$phoneNumber = '919999999999';
$phoneNumberNoCode = substr($phoneNumber, 2);
If you are dealing with phone numbers from other countries you could use preg_replace
:
$countryCode = '91'; //Change here to match country code
$phoneNumber = '919199191999';
$phoneNumberNoCode = preg_replace('/'.$countryCode.'/', '', $phoneNumber, 1);