'Remove country code from phone number?
Below is the code I am currently using to format phone numbers for databasing. However iOS is putting a +1 or 1 (country code) before it in autofills. How can I remove the 1 before the phone number?
So...
+12223334444 should be 2223334444
12223334444 should be 2223334444
preg_replace('/\D/', '', ($phone));
Solution 1:[1]
Use the following regex substitution:
if your format is : +12223334444
$country_code = '+1';
$phone_no = '+12223334444';
echo preg_replace('/^\+?1|\|1|\D/', '', ($phone_no));
Output will : 2223334444
if your format is : 12223334444
$country_code = '1';
$phone_no = '2223334444';
echo preg_replace('/^\+?1|\|1|\D/', '', ($phone_no));
Output will : 2223334444
Solution 2:[2]
This may be overkill, but here we go:
\+(?:998|996|995|994|993|992|977|976|975|974|973|972|971|970|968|967|966|965|964|963|962|961|960|886|880|856|855|853|852|850|692|691|690|689|688|687|686|685|683|682|681|680|679|678|677|676|675|674|673|672|670|599|598|597|595|593|592|591|590|509|508|507|506|505|504|503|502|501|500|423|421|420|389|387|386|385|383|382|381|380|379|378|377|376|375|374|373|372|371|370|359|358|357|356|355|354|353|352|351|350|299|298|297|291|290|269|268|267|266|265|264|263|262|261|260|258|257|256|255|254|253|252|251|250|249|248|246|245|244|243|242|241|240|239|238|237|236|235|234|233|232|231|230|229|228|227|226|225|224|223|222|221|220|218|216|213|212|211|98|95|94|93|92|91|90|86|84|82|81|66|65|64|63|62|61|60|58|57|56|55|54|53|52|51|49|48|47|46|45|44\D?1624|44\D?1534|44\D?1481|44|43|41|40|39|36|34|33|32|31|30|27|20|7|1\D?939|1\D?876|1\D?869|1\D?868|1\D?849|1\D?829|1\D?809|1\D?787|1\D?784|1\D?767|1\D?758|1\D?721|1\D?684|1\D?671|1\D?670|1\D?664|1\D?649|1\D?473|1\D?441|1\D?345|1\D?340|1\D?284|1\D?268|1\D?264|1\D?246|1\D?242|1)\D?
In order to match any prefix and since some countries could share partially same prefix (for example 1 and 1-721), you add all positibilities to the regex in descending order and size.
The regex is based on this list.
Solution 3:[3]
if (substr($request->mobile_num, 0, 2) == "98")
$mobile_num = str_replace('98', '0', $request->mobile_num);
elseif (substr($request->mobile_num, 0, 3) == "+98")
$mobile_num = str_replace('+98', '0', $request->mobile_num);
else
$mobile_num = $request->mobile_num;
Solution 4:[4]
It cleans phone number & strips (, ), -, + returns just last 10 digit numeric phone number
Examples:
1234567890 => +11234567890
1234567890 => :+112:34567890
1234567890 => Phone :+1-123-456-7890
1234567890 => Phone :+1-(123)-(456)-(7890)
1234567890 => +1-(123)-(456)-(7890)
1234567890 => +1-123-456-7890
Sample test code:
$dirty_phone = ["+11234567890", ":+112:34567890", "Phone :+1-123-456-7890", "Home :(+1)-(123)-(456)-(7890)", "+1-(123)-(456)-(7890)", "+1-123-456-7890"];
foreach ($dirty_phone as $key => $phone_no) {
$cleaned_phone = clean_phone_number($phone_no);
echo "{$cleaned_phone} => {$phone_no}<br>";
}
function clean_phone_number($phone_no){
return substr( preg_replace('/^\+?1|\|1|\D/', '', $phone_no), -10);
}
Solution 5:[5]
I have been working on credit facility USSD. The USSD captures the phone number with the country code and I wanted to send sms notification via another bulk sms API that only requires mobile number without country code. I used the below code and it works fine.
<?php
$country_code = '+254';
$phone_no = '+254712450866';
echo preg_replace('/^\+?254|\|1|\D/', '0', ($phone_no));
?>
USSD captures +254712450866 the above code formats to 0712450866
For me it works.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Julio |
Solution 3 | Sadeq |
Solution 4 | Aatef Tasneem Khan |
Solution 5 | simon maina |