I am new to php.. I want to write a script for checking the phone numbers, if they are intended to international and if they are international phone numbers I need to format them according to the E164 format. I made a regex for validating the phone number but I am stuck on formatting the phone number. Here goes the code:
<?php
$replacement = '/^\s?+(93|355|213|684|376|244|809|268|54|374|297|247|61|672|43|994|242|246|973|880|375|32|501|229|809|975|284|591|387|267|55|284|673|359|226|257|855|237|1|238|1|345|238|236|235|56|86|886|57|269|242|682|506|385|53|357|420|45|246|767|809|253|593|20|503|240|291|372|251|500|298|679|358|33|596|594|241|220|995|49|233|350|30|299|473|671|502|224|245|502|509|504|852|36|354|91|62|98|964|353|972|39|225|876|81|962|7|254|855|686|82|850|965|996|371|856|961|266|231|370|218|423|353|853|389|261|265|60|960|223|356|692|596|222|230|269|52|691|373|33|976|473|212|258|95|264|674|977|31|599|869|687|64|505|227|234|683|850|1670|47|968|92|680|507|675|595|51|63|48|351|1787|974|262|40|7|250|670|378|239|966|221|381|248|232|65|421|386|677|252|27|34|94|290|869|508|249|597|268|46|41|963|689|886|7|255|66|228|690|676|1868|90|993|688|256|380|971|44|598|1|7|678|39|58|84|1340|681|685|381|967|381|243|260|263)?\s?[.]?[-]?[(]?\d{3}[)]?\s?[.]?[-]?\d{3}\s?[.]?[-]?\d+/'; $phonenumber = '+1 (123) 456-7890';
if(preg_match($replacement, $phonenumber, $matches))
{
echo "matched";
} else print_r($matches);
?>
E164 is a complex undertaking because its a moving target. Having said that google is doing an admirable job with their libphonenumber. However it's not for PHP. However Joshua Gigg was so kind to port this to PHP with his libphonenumber-for-php and this is what I use. He is keeping it updated and in sync with the google version. There is a online demo available https://giggsey.com/libphonenumber
To answer your question specifically:
Install using composer with composer require giggsey/libphonenumber-for-php
.
Let's say you have a string representing a phone number from Switzerland. This is how you parse/normalize it into a PhoneNumber object:
$swissNumberStr = "044 668 18 00";
$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
try {
$swissNumberProto = $phoneUtil->parse($swissNumberStr, "CH");
var_dump($swissNumberProto);
} catch (\libphonenumber\NumberParseException $e) {
var_dump($e);
}
At this point, swissNumberProto contains:
class libphonenumber\PhoneNumber#9 (7) {
private $countryCode =>
int(41)
private $nationalNumber =>
double(446681800)
private $extension =>
NULL
private $italianLeadingZero =>
NULL
private $rawInput =>
NULL
private $countryCodeSource =>
NULL
private $preferredDomesticCarrierCode =>
NULL
}
Now let us validate whether the number is valid:
$isValid = $phoneUtil->isValidNumber($swissNumberProto);
var_dump($isValid); // true
This example uses a region code (CH) to format the number. This however is not necessary if the number is passed in an international format (e.g. +44 117 496 0123).
There are many, many more option to this utility and I encourage you to dive into the documentation. There is really only so much that can be done in a quick answer. I really hope that helps you and anyone else who runs across this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With