Download the PHP package brick/phonenumber without Composer
On this page you can find all versions of the php package brick/phonenumber. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download brick/phonenumber
More information about brick/phonenumber
Files in brick/phonenumber
Informations about the package phonenumber
Brick\PhoneNumber
A phone number library for PHP.
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 encapsulatesPhoneNumberUtil
;PhoneNumberFormat
andPhoneNumberType
are native PHP enums.
Installation
This library is installable via Composer:
Requirements
This library requires PHP 8.1 or later.
For PHP 7.4 and PHP 8.0 support, use version 0.5
.
For PHP 7.1 support, use version 0.4
.
For PHP 5.6 and PHP 7.0 support, use version 0.1
.
Note that these PHP versions are EOL and not supported anymore. If you're still using one of these PHP versions, you should consider upgrading as soon as possible.
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.6.*
.
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('+33123456789')
; - 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
:
In most cases, it is recommended to perform an extra step of validation with isValidNumber()
or isPossibleNumber()
:
As a rule of thumb, do the following:
- When the number comes from user input, do a full validation:
parse()
and catchPhoneNumberParseException
, then callisValidNumber()
(orisPossibleNumber()
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 a PhoneNumberFormat enum value:
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 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 PhoneNumberType enum value:
If the type is unknown, the PhoneNumberType::UNKNOWN
value is returned.
Check the PhoneNumberType
enum for all possible values.
Number information
You can extract the following information from a phone number:
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:
The return type of getExampleNumber()
is a PhoneNumber
instance, so you can format it as you like:
If no example phone number is available for the country code / number type combination, a PhoneNumberException
is thrown.
Casting to string
Casting a PhoneNumber
to string returns its E164 representation (+
followed by digits), so the following are equivalent:
You can serialize a PhoneNumber
to string, then recover it using parse()
without a country code:
Doctrine mappings
You can use PhoneNumber
objects in your Doctrine entities using the brick/phonenumber-doctrine package.