Remove country code from phone number?
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
Trim country code from phone number in php
Change start of string with sign + and country code
$result = preg_replace("/^\+?{$country_code}/", '',$phone_no);
demo
Remove only country code from string not slahsh?
Use the following regex substitution:
$result = preg_replace("/^\+\d+-/", "", $sPhoneNumber);
It will remove 1+ digits and a -
after it from the start of a string.
Using this solution, you will avoid changing strings that do not start with +
followed with digits.
Instead of \d+
you may specify the country codes you need to remove. Say, you need to remove 22
and 48
codes:
$result = preg_replace("/^\+(?:48|22)-/", "", $sPhoneNumber);
Format a Phone Number (Detect and add the country code if missing)
Here is your regexp:
^(?:\+27|27|0)?(\d*)
and if it match something use this: +27$1
Here is working example (improved): https://regex101.com/r/VaUAKN/7
EXPLANATION:
First we wanna split prefix (if exists) with number: ^(?:\+27|27|0)?
the ?:
inside group makes that prefix group will be ignored in results. The ^
means begining of line. The last ?
allow prefix to not exists. And at the end (\d*)
catch numbers after prefix
PHP working example (Wiktor Stribiżew noted that in regexp we can can omit (\d*)
and $1
and change \+27|27
to \+?27
):
$numOld = "2712345678";
$num = preg_replace('/^(?:\+?27|0)?/','+27', $numOld); // > $num = "+2712345678"
Trim the first digit of a Phone number to be in International format
You don't even need to invoke regex here, substr()
should work just fine:
$input = "07123456789";
$output = "254" . substr($input, 1);
If you only want to do this replacement on numbers beginning with zero, then it might make more sense to use preg_replace
:
$output = preg_replace("/^0/", "254", $input);
Extracting Country Code From Mobile Number
I might be wrong but i highly doubt this is possible mainly because of legal issues(reverse check will not work i guess). One way I think you could manage to pull this off is to ask for country of origin when asking users to write down their phone number.
Something like a dropdown of countries just before the phone number input field, and in the backend you could build the full phone number by adding the prefix according to the selected country.
Also, to make sure they don't select country and then also write down the prefix, validate the phone number field with some regex, max 10 digits, or something like that.
I hope this helps and if I find something better, I'll keep you updated. Cheers!
How to turn a phone number into a new form (with area codes)
Use preg_replace()
to transform phone number to desired output:
$phone = '+4407111111111';
$newPhone = preg_replace('/^\+([1-9]+)0(\d+)$/','$1$2',$phone);
echo $newPhone;
Explanation of regular expression:
- ^ = Start at beginning of string
- + = match a "+"
- ([1-9]+) = match a sequence of digits from 1 to 9 and assign them to $1
- 0 = match one "0"
- (\d+)$ = match all digits up to the end of the string and assign match to $2
Replacement is simple, just use "$1$2" as defined in above explanation.
Better than trying to split up a given phone numer is to fetch data for country code, area code and phone number as seperate values. Makes things much easier and delivers better results because phone numbers may be handled a little bit different from country to country.
Related Topics
How to Capitalize First Letter in Laravel Blade
How to Count Occurrence of Duplicate Items in Array
How to Not Make Phpmailer Send an Email With Multiple 'To' Addresses
Block Direct Access to a File Over Http But Allow PHP Script Access
How to Update Stock Quantity Using MySQL in PHP
Laravel 5.5 Http Error 500 Unable to Handle This Request
Object of Class Mysqli_Result Could Not Be Converted to String
Redirect That Same Page from Where Id Is Coming
Trim Country Code from Phone Number in PHP
Saving Multiple Records in a Laravel Eloquent Create
Getting Data from a Textarea into a Database
Session Id Is Changing After Every Request
How to Fetch Data After Selecting from Dropdown List
How to Set the Default Value of an Attribute on a Laravel Model
Using Maatwebsite Excel 3.1 Export How to Set Background Color for Cells and Font Size for Heading
How to Delete Multiple Redis Keys With the Same Pattern in PHP Using Phpredis