• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..26-Nov-2021-

src/H26-Nov-2021-559,042540,769

LICENSEH A D26-Nov-20219.9 KiB178150

README.mdH A D26-Nov-202111 KiB264187

composer.jsonH A D26-Nov-20211.9 KiB8684

README.md

1# libphonenumber for PHP [![Build Status](https://github.com/giggsey/libphonenumber-for-php/workflows/Continuous%20Integration/badge.svg)](https://github.com/giggsey/libphonenumber-for-php/actions?query=workflow%3A%22Continuous+Integration%22) [![Coverage Status](https://img.shields.io/coveralls/giggsey/libphonenumber-for-php.svg?style=flat-square)](https://coveralls.io/r/giggsey/libphonenumber-for-php?branch=master)
2
3[![Total Downloads](https://poser.pugx.org/giggsey/libphonenumber-for-php/downloads?format=flat-square)](https://packagist.org/packages/giggsey/libphonenumber-for-php)
4[![Downloads per month](https://img.shields.io/packagist/dm/giggsey/libphonenumber-for-php.svg?style=flat-square)](https://packagist.org/packages/giggsey/libphonenumber-for-php)
5[![Latest Stable Version](https://img.shields.io/packagist/v/giggsey/libphonenumber-for-php.svg?style=flat-square)](https://packagist.org/packages/giggsey/libphonenumber-for-php)
6[![License](https://img.shields.io/badge/license-Apache%202.0-red.svg?style=flat-square)](https://packagist.org/packages/giggsey/libphonenumber-for-php)
7
8## What is it?
9A PHP library for parsing, formatting, storing and validating international phone numbers. This library is based on Google's [libphonenumber](https://github.com/google/libphonenumber).
10
11 - [Installation](#installation)
12 - [Documentation](#documentation)
13 - [Online Demo](#online-demo)
14 - [Highlights of functionality](#highlights-of-functionality)
15   - [Versioning](#versioning)
16   - [Quick Examples](#quick-examples)
17     - [Geocoder](#geocoder)
18     - [ShortNumberInfo](#shortnumberinfo)
19     - [Mapping Phone Numbers to carrier](#mapping-phone-numbers-to-carrier)
20     - [Mapping Phone Numbers to TimeZones](#mapping-phone-numbers-to-timezones)
21 - [FAQ](#faq)
22   - [Problems with Invalid Numbers?](#problems-with-invalid-numbers)
23 - [Generating data](#generating-data)
24 - [Integration with frameworks](#integration-with-frameworks)
25
26
27## Installation
28
29PHP versions 5.3 up to PHP 8.0 are currently supported.
30
31The PECL [mbstring](http://php.net/mbstring) extension is required.
32
33It is recommended to use [composer](https://getcomposer.org) to install the library.
34
35```bash
36$ composer require giggsey/libphonenumber-for-php
37```
38
39You can also use any other [PSR-4](http://www.php-fig.org/psr/psr-4/) compliant autoloader.
40
41If you do not use composer, ensure that you also load any dependencies that this project has, such as [giggsey/locale](https://github.com/giggsey/Locale).
42
43## Documentation
44
45 - [PhoneNumber Util](docs/PhoneNumberUtil.md)
46 - [ShortNumber Info](docs/ShortNumberInfo.md)
47 - [Phone Number Geolocation](docs/PhoneNumberOfflineGeocoder.md)
48 - [Phone Number to Carrier Mapping](docs/PhoneNumberToCarrierMapper.md)
49 - [Phone Number to Timezone Mapping](docs/PhoneNumberToTimeZonesMapper.md)
50 - [Phone Number Matcher](docs/PhoneNumberMatcher.md)
51 - [As You Type Formatter](docs/AsYouTypeFormatter.md)
52
53## Online Demo
54An [online demo](http://giggsey.com/libphonenumber/) is available, and the source can be found at [giggsey/libphonenumber-example](https://github.com/giggsey/libphonenumber-example).
55
56# Highlights of functionality
57* Parsing/formatting/validating phone numbers for all countries/regions of the world.
58* `getNumberType` - gets the type of the number based on the number itself; able to distinguish Fixed-line, Mobile, Toll-free, Premium Rate, Shared Cost, VoIP and Personal Numbers (whenever feasible).
59* `isNumberMatch` - gets a confidence level on whether two numbers could be the same.
60* `getExampleNumber`/`getExampleNumberByType` - provides valid example numbers for all countries/regions, with the option of specifying which type of example phone number is needed.
61* `isValidNumber` - full validation of a phone number for a region using length and prefix information.
62* `PhoneNumberOfflineGeocoder` - provides geographical information related to a phone number.
63* `PhoneNumberToTimeZonesMapper` - provides timezone information related to a phone number.
64* `PhoneNumberToCarrierMapper` - provides carrier information related to a phone number.
65
66## Versioning
67
68This library will try to follow the same version numbers as Google. There could be additional releases where needed to fix critical issues that can not wait until the next release from Google.
69
70This does mean that this project may not follow [Semantic Versioning](http://semver.org/), but instead Google's version policy. As a result, jumps in major versions may not actually contain any backwards
71incompatible changes. Please read the release notes for such releases.
72
73Google try to release their versions according to Semantic Versioning, as laid out of in their [Versioning Guide](https://github.com/google/libphonenumber#versioning-and-announcements).
74
75## Quick Examples
76Let's say you have a string representing a phone number from Switzerland. This is how you parse/normalize it into a PhoneNumber object:
77
78```php
79$swissNumberStr = "044 668 18 00";
80$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
81try {
82    $swissNumberProto = $phoneUtil->parse($swissNumberStr, "CH");
83    var_dump($swissNumberProto);
84} catch (\libphonenumber\NumberParseException $e) {
85    var_dump($e);
86}
87```
88
89At this point, swissNumberProto contains:
90
91    class libphonenumber\PhoneNumber#9 (7) {
92     private $countryCode =>
93      int(41)
94     private $nationalNumber =>
95      double(446681800)
96     private $extension =>
97      NULL
98     private $italianLeadingZero =>
99      NULL
100     private $rawInput =>
101      NULL
102     private $countryCodeSource =>
103      NULL
104     private $preferredDomesticCarrierCode =>
105      NULL
106    }
107
108Now let us validate whether the number is valid:
109
110```php
111$isValid = $phoneUtil->isValidNumber($swissNumberProto);
112var_dump($isValid); // true
113```
114
115There are a few formats supported by the formatting method, as illustrated below:
116
117```php
118// Produces "+41446681800"
119echo $phoneUtil->format($swissNumberProto, \libphonenumber\PhoneNumberFormat::E164);
120
121// Produces "044 668 18 00"
122echo $phoneUtil->format($swissNumberProto, \libphonenumber\PhoneNumberFormat::NATIONAL);
123
124// Produces "+41 44 668 18 00"
125echo $phoneUtil->format($swissNumberProto, \libphonenumber\PhoneNumberFormat::INTERNATIONAL);
126```
127
128You could also choose to format the number in the way it is dialled from another country:
129
130```php
131// Produces "011 41 44 668 1800", the number when it is dialled in the United States.
132echo $phoneUtil->formatOutOfCountryCallingNumber($swissNumberProto, "US");
133
134// Produces "00 41 44 668 18 00", the number when it is dialled in Great Britain.
135echo $phoneUtil->formatOutOfCountryCallingNumber($swissNumberProto, "GB");
136```
137
138### Geocoder
139
140```php
141$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
142
143$swissNumberProto = $phoneUtil->parse("044 668 18 00", "CH");
144$usNumberProto = $phoneUtil->parse("+1 650 253 0000", "US");
145$gbNumberProto = $phoneUtil->parse("0161 496 0000", "GB");
146
147$geocoder = \libphonenumber\geocoding\PhoneNumberOfflineGeocoder::getInstance();
148
149// Outputs "Zurich"
150echo $geocoder->getDescriptionForNumber($swissNumberProto, "en_US");
151
152// Outputs "Zürich"
153echo $geocoder->getDescriptionForNumber($swissNumberProto, "de_DE");
154
155// Outputs "Zurigo"
156echo $geocoder->getDescriptionForNumber($swissNumberProto, "it_IT");
157
158// Outputs "Mountain View, CA"
159echo $geocoder->getDescriptionForNumber($usNumberProto, "en_US");
160
161// Outputs "Mountain View, CA"
162echo $geocoder->getDescriptionForNumber($usNumberProto, "de_DE");
163
164// Outputs "미국" (Korean for United States)
165echo $geocoder->getDescriptionForNumber($usNumberProto, "ko-KR");
166
167// Outputs "Manchester"
168echo $geocoder->getDescriptionForNumber($gbNumberProto, "en_GB");
169
170// Outputs "영국" (Korean for United Kingdom)
171echo $geocoder->getDescriptionForNumber($gbNumberProto, "ko-KR");
172```
173
174### ShortNumberInfo
175
176```php
177$shortNumberInfo = \libphonenumber\ShortNumberInfo::getInstance();
178
179// true
180var_dump($shortNumberInfo->isEmergencyNumber("999", "GB"));
181
182// true
183var_dump($shortNumberInfo->connectsToEmergencyNumber("999", "GB"));
184
185// false
186var_dump($shortNumberInfo->connectsToEmergencyNumber("911", "GB"));
187
188// true
189var_dump($shortNumberInfo->isEmergencyNumber("911", "US"));
190
191// true
192var_dump($shortNumberInfo->connectsToEmergencyNumber("911", "US"));
193
194// false
195var_dump($shortNumberInfo->isEmergencyNumber("911123", "US"));
196
197// true
198var_dump($shortNumberInfo->connectsToEmergencyNumber("911123", "US"));
199```
200
201### Mapping Phone Numbers to carrier
202
203```php
204$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
205$swissNumberProto = $phoneUtil->parse("798765432", "CH");
206
207$carrierMapper = \libphonenumber\PhoneNumberToCarrierMapper::getInstance();
208// Outputs "Swisscom"
209echo $carrierMapper->getNameForNumber($swissNumberProto, "en");
210```
211
212### Mapping Phone Numbers to TimeZones
213
214```php
215$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
216$swissNumberProto = $phoneUtil->parse("798765432", "CH");
217
218$timeZoneMapper = \libphonenumber\PhoneNumberToTimeZonesMapper::getInstance();
219// returns array("Europe/Zurich")
220$timeZones = $timeZoneMapper->getTimeZonesForNumber($swissNumberProto);
221```
222
223## FAQ
224
225#### Problems with Invalid Numbers?
226
227This library uses phone number metadata from Google's [libphonenumber](https://github.com/google/libphonenumber). If this library is working as intended, it should provide the same result as the Java version of Google's project.
228
229If you believe that a phone number is returning an incorrect result, first test it with [libphonenumber](https://github.com/google/libphonenumber) via their [Online Demo](https://libphonenumber.appspot.com/). If that returns the same result as this project, and you feel it is in error, raise it as an Issue with the libphonenumber project.
230
231If Google's [Online Demo](https://libphonenumber.appspot.com/) gives a different result to the [libphonenumber-for-php demo](http://giggsey.com/libphonenumber/), then please raise an Issue here.
232
233## Generating data
234
235Generating the data is not normally needed, as this repository will generally always have the up to data metadata.
236
237If you do need to generate the data, the commands are provided by [Phing](https://www.phing.info). Ensure you have all the dev composer dependencies installed, then run
238
239```bash
240$ vendor/bin/phing compile
241```
242
243This compile process clones the [libphonenumber](https://github.com/google/libphonenumber) project at the version specified in [METADATA-VERSION.txt](METADATA-VERSION.txt).
244
245### Running tests
246
247This project uses [PHPUnit Bridge](https://symfony.com/doc/current/components/phpunit_bridge.html) to maintain compatibility for the supported PHP versions.
248
249To run the tests locally, run the `./phpunit` script.
250
251## Integration with frameworks
252
253Other packages exist that integrate libphonenumber-for-php into frameworks.
254
255| Framework | Packages      |
256| --------- |:-------------:|
257|Symfony|[PhoneNumberBundle](https://github.com/odolbeau/phone-number-bundle)|
258|Laravel|[Laravel Phone](https://github.com/Propaganistas/Laravel-Phone)|
259|Yii2|[PhoneInput](https://github.com/Borales/yii2-phone-input)|
260|Kohana|[PhoneNumber](https://github.com/softmediadev/kohana-phonenumber)|
261|TYPO3|[TYPO3 Phone Extension](https://github.com/simonschaufi/typo3-phone)|
262
263These packages are supplied by third parties, and their quality can not be guaranteed.
264