PHP code example of giggsey / libphonenumber-for-php
1. Go to this page and download the library: Download giggsey/libphonenumber-for-php library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once ('vendor/autoload.php' );
giggsey / libphonenumber-for-php example snippets
$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);
}
$isValid = $phoneUtil->isValidNumber($swissNumberProto);
var_dump($isValid);
echo $phoneUtil->format($swissNumberProto, \libphonenumber\PhoneNumberFormat::E164);
echo $phoneUtil->format($swissNumberProto, \libphonenumber\PhoneNumberFormat::NATIONAL);
echo $phoneUtil->format($swissNumberProto, \libphonenumber\PhoneNumberFormat::INTERNATIONAL);
echo $phoneUtil->formatOutOfCountryCallingNumber($swissNumberProto, "US" );
echo $phoneUtil->formatOutOfCountryCallingNumber($swissNumberProto, "GB" );
$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
$swissNumberProto = $phoneUtil->parse("044 668 18 00" , "CH" );
$usNumberProto = $phoneUtil->parse("+1 650 253 0000" , "US" );
$gbNumberProto = $phoneUtil->parse("0161 496 0000" , "GB" );
$geocoder = \libphonenumber\geocoding\PhoneNumberOfflineGeocoder::getInstance();
echo $geocoder->getDescriptionForNumber($swissNumberProto, "en_US" );
echo $geocoder->getDescriptionForNumber($swissNumberProto, "de_DE" );
echo $geocoder->getDescriptionForNumber($swissNumberProto, "it_IT" );
echo $geocoder->getDescriptionForNumber($usNumberProto, "en_US" );
echo $geocoder->getDescriptionForNumber($usNumberProto, "de_DE" );
echo $geocoder->getDescriptionForNumber($usNumberProto, "ko-KR" );
echo $geocoder->getDescriptionForNumber($gbNumberProto, "en_GB" );
echo $geocoder->getDescriptionForNumber($gbNumberProto, "ko-KR" );
$shortNumberInfo = \libphonenumber\ShortNumberInfo::getInstance();
var_dump($shortNumberInfo->isEmergencyNumber("999" , "GB" ));
var_dump($shortNumberInfo->connectsToEmergencyNumber("999" , "GB" ));
var_dump($shortNumberInfo->connectsToEmergencyNumber("911" , "GB" ));
var_dump($shortNumberInfo->isEmergencyNumber("911" , "US" ));
var_dump($shortNumberInfo->connectsToEmergencyNumber("911" , "US" ));
var_dump($shortNumberInfo->isEmergencyNumber("911123" , "US" ));
var_dump($shortNumberInfo->connectsToEmergencyNumber("911123" , "US" ));
$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
$swissNumberProto = $phoneUtil->parse("798765432" , "CH" );
$carrierMapper = \libphonenumber\PhoneNumberToCarrierMapper::getInstance();
echo $carrierMapper->getNameForNumber($swissNumberProto, "en" );
$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
$swissNumberProto = $phoneUtil->parse("798765432" , "CH" );
$timeZoneMapper = \libphonenumber\PhoneNumberToTimeZonesMapper::getInstance();
$timeZones = $timeZoneMapper->getTimeZonesForNumber($swissNumberProto);
bash
composer