All Projects → brick → Phonenumber

brick / Phonenumber

Licence: mit
A phone number library for PHP

Projects that are alternatives of or similar to Phonenumber

Vue Phone Number Input
A phone number input made with Vue JS (format & valid phone number)
Stars: ✭ 407 (+146.67%)
Mutual labels:  phone-number
Phonelib
Ruby gem for phone validation and formatting using google libphonenumber library data
Stars: ✭ 731 (+343.03%)
Mutual labels:  phone-number
Wbm
wbm is an unofficial API to send bulk messages in whatsapp.
Stars: ✭ 55 (-66.67%)
Mutual labels:  phone-number
Phoneinfoga
PhoneInfoga is one of the most advanced tools to scan international phone numbers using only free resources. It allows you to first gather standard information such as country, area, carrier and line type on any international phone number. Then search for footprints on search engines to try to find the VoIP provider or identify the owner.
Stars: ✭ 5,927 (+3492.12%)
Mutual labels:  phone-number
Phone
With a given country and phone number, validate and reformat the mobile phone number to the E.164 standard. The purpose of this is to allow us to send SMS to mobile phones only.
Stars: ✭ 531 (+221.82%)
Mutual labels:  phone-number
Fieldtypephone
ProcessWire Fieldtype for entering 4 part phone numbers: country/area code/number/extension as integers
Stars: ✭ 5 (-96.97%)
Mutual labels:  phone-number
Libphonenumber For Php
PHP version of Google's phone number handling library
Stars: ✭ 3,938 (+2286.67%)
Mutual labels:  phone-number
Ngx Intl Tel Input
Phone number input field to support international numbers, Angular
Stars: ✭ 140 (-15.15%)
Mutual labels:  phone-number
Intl Tel Input
A JavaScript plugin for entering and validating international telephone numbers
Stars: ✭ 5,963 (+3513.94%)
Mutual labels:  phone-number
Numpad
Numpad is light weight library for multipurpose usage in numaric input
Stars: ✭ 44 (-73.33%)
Mutual labels:  phone-number
Dial2verify Twilio
Phone verification at no cost
Stars: ✭ 432 (+161.82%)
Mutual labels:  phone-number
Chinamobilephonenumberregex
Regular expressions that match the mobile phone number in mainland China. / 一组匹配中国大陆手机号码的正则表达式。
Stars: ✭ 4,440 (+2590.91%)
Mutual labels:  phone-number
Afaddressbookmanager
Get contacts from iOS Address Book by their phone numbers and email addresses. Works on iOS 6+.
Stars: ✭ 24 (-85.45%)
Mutual labels:  phone-number
Edittext Mask
The custom masks for EditText. The solution for input phone numbers, SSN, and so on for Android
Stars: ✭ 413 (+150.3%)
Mutual labels:  phone-number
Deadtrap
An OSINT tool to gather information about the real owner of a phone number
Stars: ✭ 73 (-55.76%)
Mutual labels:  phone-number
Libphonenumber Csharp
Offical C# port of https://github.com/googlei18n/libphonenumber
Stars: ✭ 396 (+140%)
Mutual labels:  phone-number
Crboxinputview
Verify code input view. Support security type for password.短信验证码输入框,支持密文模式
Stars: ✭ 749 (+353.94%)
Mutual labels:  phone-number
React Native Unified Contacts
Your best friend when working with the latest and greatest Contacts Framework in iOS 9+ in React Native.
Stars: ✭ 156 (-5.45%)
Mutual labels:  phone-number
Mini phone
A fast phone number lib for Ruby (binds to Google's C++ libphonenumber)
Stars: ✭ 131 (-20.61%)
Mutual labels:  phone-number
Laravel Sms
📱✔️A phone number validation solution based on laravel
Stars: ✭ 838 (+407.88%)
Mutual labels:  phone-number

Brick\PhoneNumber

A phone number library for PHP.

Build Status Coverage Status Latest Stable Version Total Downloads License

This library is a thin wrapper around giggsey/libphonenumber-for-php, itself a port of Google's libphonenumber.

It provides an equivalent functionality, with the following implementation differences:

  • PhoneNumber is an immutable class; it can be safely passed around without having to worry about the risk for it to be changed;
  • PhoneNumber is not just a mere data container, but provides all the methods to parse, format and validate phone numbers; it transparently encapsulates PhoneNumberUtil.

Installation

This library is installable via Composer:

composer require brick/phonenumber

Requirements

This library requires PHP 7.1 or later. for PHP 5.6 and PHP 7.0 support, use version 0.1.

Project status & release process

While this library is still under development, it is well tested and should be stable enough to use in production environments.

The current releases are numbered 0.x.y. When a non-breaking change is introduced (adding new methods, optimizing existing code, etc.), y is incremented.

When a breaking change is introduced, a new 0.x version cycle is always started.

It is therefore safe to lock your project to a given release cycle, such as 0.2.*.

If you need to upgrade to a newer release cycle, check the release history for a list of changes introduced by each further 0.x.0 version.

Quick start

All the classes lie in the Brick\PhoneNumber namespace.

To obtain an instance of PhoneNumber, use the parse() method:

  • Using an international number: PhoneNumber::parse('+336123456789');
  • Using a national number and a country code: PhoneNumber::parse('01 23 45 67 89', 'FR');

Validating a number

The parse() method is quite permissive with numbers; it basically attempts to match a country code, and validates the length of the phone number for this country.

If a number is really malformed, it throws a PhoneNumberParseException:

use Brick\PhoneNumber\PhoneNumber;
use Brick\PhoneNumber\PhoneNumberParseException;

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

In most cases, it is recommended to perform an extra step of validation with isValidNumber() or isPossibleNumber():

if (! $number->isPossibleNumber()) {
    // a more lenient and faster check than `isValidNumber()`
}

if (! $number->isValidNumber()) {
    // strict check relying on up-to-date metadata library
}

As a rule of thumb, do the following:

  • When the number comes from user input, do a full validation: parse() and catch PhoneNumberParseException, then call isValidNumber() (or isPossibleNumber() for a more lenient check) if no exception occurred;
  • When the number is later retrieved from your database, and has been validated before, you can just perform a blind parse().

Formatting a number

Basic formatting

You can use format() with constants from the PhoneNumberFormat class:

$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

Formatting to call from another country

You may want to present a phone number to an audience in a specific country, with the correct international prefix when required. This is what formatForCallingFrom() does:

$number = PhoneNumber::parse('+447123456789');
$number->formatForCallingFrom('GB'); // 07123 456789
$number->formatForCallingFrom('FR'); // 00 44 7123 456789
$number->formatForCallingFrom('US'); // 011 44 7123 456789

Number types

In certain cases, it is possible to know the type of a phone number (fixed line, mobile phone, etc.), using the getNumberType() method, which returns a constant from the PhoneNumberType class:

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

If the type is unknown, the PhoneNumberType::UNKNOWN value is returned. Check the PhoneNumberType class for all possible values.

Number information

You can extract the following information from a phone number:

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

Example numbers

You can get an example number for a country code and an optional number type (defaults to fixed line). This can be useful to use as a placeholder in an input field, for example:

echo PhoneNumber::getExampleNumber('FR'); // +33123456789
echo PhoneNumber::getExampleNumber('FR', PhoneNumberType::MOBILE); // +33612345678

The return type of getExampleNumber() is a PhoneNumber instance, so you can format it as you like:

echo PhoneNumber::getExampleNumber('FR')->formatForCallingFrom('FR'); // 01 23 45 67 89

If no example phone number is available for the country code / number type combination, a PhoneNumberException is thrown.

brick/phonenumber for enterprise

Available as part of the Tidelift Subscription.

The maintainers of brick/phonenumber and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. Learn more.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].