David Zalmann
David Zalmann

Reputation: 119

formatting local phone numbers into international (knowing countries)

I use an SMS API to send confirmation code to phone.
Its parameters requires me to set the visitors phone number format into international, for example +9912345678 instead of 012 345 678.
Is there a php class (without Composer) that would do it for me (could not find anything so far on GG), considering I know user's both country (from a select input) and number (from a text input) he submitted on the previous webpage?

Upvotes: 3

Views: 6983

Answers (4)

Zoltán Süle
Zoltán Süle

Reputation: 1694

I recommend to use brick/phonenumber for validating and formatting national and international phone numbers.

It is up-to-date and PHP 8.x compatible with useful methods. You can use it with and without composer.

Validating a number

try {
    $number = PhoneNumber::parse('+333');
}
catch (PhoneNumberParseException $e) {
    // 'The string supplied is too short to be a phone number.'
}

Formatting a number

$number = PhoneNumber::parse('+41446681800');
$number->format(PhoneNumberFormat::E164); // +41446681800
$number->format(PhoneNumberFormat::INTERNATIONAL); // +41 44 668 18 00
$number->format(PhoneNumberFormat::NATIONAL); // 044 668 18 00
$number->format(PhoneNumberFormat::RFC3966); // tel:+41-44-668-18-00

Number types

PhoneNumber::parse('+336123456789')->getNumberType(); // PhoneNumberType::MOBILE
PhoneNumber::parse('+33123456789')->getNumberType(); // PhoneNumberType::FIXED_LINE

Number information

$number = PhoneNumber::parse('+447123456789');
echo $number->getRegionCode(); // GB
echo $number->getCountryCode(); // 44
echo $number->getNationalNumber(); // 7123456789

Upvotes: 0

Meloman
Meloman

Reputation: 3712

Based on @Huso answer, I wrote the following function to format and sanitize (remove dashes, dots and spaces) phone numbers :

function phonize($phoneNumber, $country) {

    $countryCodes = array(
        'ch' => '+41',
        'de' => '+43',
        'it' => '+39'
    );

    return preg_replace('/[^0-9+]/', '',
           preg_replace('/^0/', $countryCodes[$country], $phoneNumber));
}

Use :

$phone   = '022 123 43 65';
$country = 'ch';

$phone   = phonize($phone, $country);

Upvotes: 2

Daniel Dudas
Daniel Dudas

Reputation: 3002

I previously used this library (without composer) and worked well: https://github.com/davideme/libphonenumber-for-PHP

You only have to include in your php PhoneNumberUtil.php and that file knows what else needs to include.

You can format a number like this:

$swissNumberStr = "044 668 18 00";
$phoneUtil = PhoneNumberUtil::getInstance();
try {
    $swissNumberProto = $phoneUtil->parseAndKeepRawInput($swissNumberStr, "CH");
    var_dump($swissNumberProto);
} catch (NumberParseException $e) {
    echo $e;
}
echo $phoneUtil->format($swissNumberProto, PhoneNumberFormat::INTERNATIONAL)

Check demo.php to see more examples on how to use library.

Upvotes: 3

Huso
Huso

Reputation: 1486

You can do this very easily by just replacing the 0 with the country code.

Example:

$originalNumber = '012345678';
$countryCode = '+99'; // Replace with known country code of user.
$internationalNumber = preg_replace('/^0/', $countryCode, $originalNumber);
echo $internationalNumber; // Will output: +9912345678

Hope this helps.

Upvotes: 3

Related Questions