11767c5feSopenharmony_ci<p align="right"> 21767c5feSopenharmony_ci<img src="https://travis-ci.org/google/libphonenumber.svg?branch=master"> 31767c5feSopenharmony_ci</p> 41767c5feSopenharmony_ci 51767c5feSopenharmony_ci# What is it? 61767c5feSopenharmony_ci 71767c5feSopenharmony_ciGoogle's common Java, C++ and JavaScript library for parsing, formatting, and 81767c5feSopenharmony_civalidating international phone numbers. The Java version is optimized for 91767c5feSopenharmony_cirunning on smartphones, and is used by the Android framework since 4.0 (Ice 101767c5feSopenharmony_ciCream Sandwich). 111767c5feSopenharmony_ci 121767c5feSopenharmony_ci# Quick links 131767c5feSopenharmony_ci 141767c5feSopenharmony_ci* **Reporting an issue?** Want to send a pull request? See the [contribution 151767c5feSopenharmony_ci guidelines](CONTRIBUTING.md) 161767c5feSopenharmony_ci* Check the [frequently asked questions](FAQ.md) 171767c5feSopenharmony_ci* Fun! [Falsehoods Programmers Believe About Phone Numbers](FALSEHOODS.md) 181767c5feSopenharmony_ci* Look for 191767c5feSopenharmony_ci [`README`s](https://github.com/google/libphonenumber/find/master) in 201767c5feSopenharmony_ci directories relevant to the code you're interested in. 211767c5feSopenharmony_ci* For contributors and porters: [How to run the Java demo](run-java-demo.md) 221767c5feSopenharmony_ci* For porters: [How to make metadata changes](making-metadata-changes.md) 231767c5feSopenharmony_ci 241767c5feSopenharmony_ci# Highlights of functionality 251767c5feSopenharmony_ci 261767c5feSopenharmony_ci* Parsing, formatting, and validating phone numbers for all countries/regions 271767c5feSopenharmony_ci of the world. 281767c5feSopenharmony_ci* `getNumberType` - gets the type of the number based on the number itself; 291767c5feSopenharmony_ci able to distinguish Fixed-line, Mobile, Toll-free, Premium Rate, Shared 301767c5feSopenharmony_ci Cost, VoIP, Personal Numbers, UAN, Pager, and Voicemail (whenever feasible). 311767c5feSopenharmony_ci* `isNumberMatch` - gets a confidence level on whether two numbers could be 321767c5feSopenharmony_ci the same. 331767c5feSopenharmony_ci* `getExampleNumber` and `getExampleNumberForType` - provide valid example 341767c5feSopenharmony_ci numbers for all countries/regions, with the option of specifying which type 351767c5feSopenharmony_ci of example phone number is needed. 361767c5feSopenharmony_ci* `isPossibleNumber` - quickly guesses whether a number is a possible 371767c5feSopenharmony_ci phone number by using only the length information, much faster than a full 381767c5feSopenharmony_ci validation. 391767c5feSopenharmony_ci* `isValidNumber` - full validation of a phone number for a region using 401767c5feSopenharmony_ci length and prefix information. 411767c5feSopenharmony_ci* `AsYouTypeFormatter` - formats phone numbers on-the-fly when users enter 421767c5feSopenharmony_ci each digit. 431767c5feSopenharmony_ci* `findNumbers` - finds numbers in text. 441767c5feSopenharmony_ci* `PhoneNumberOfflineGeocoder` - provides geographical information related to 451767c5feSopenharmony_ci a phone number. 461767c5feSopenharmony_ci* `PhoneNumberToCarrierMapper` - provides carrier information related to a 471767c5feSopenharmony_ci phone number. 481767c5feSopenharmony_ci* `PhoneNumberToTimeZonesMapper` - provides timezone information related to a 491767c5feSopenharmony_ci phone number. 501767c5feSopenharmony_ci 511767c5feSopenharmony_ci# Demo 521767c5feSopenharmony_ci 531767c5feSopenharmony_ci## Java 541767c5feSopenharmony_ci 551767c5feSopenharmony_ciThe [Java demo](https://libphonenumber.appspot.com/) is updated with a slight 561767c5feSopenharmony_cidelay after the GitHub release. 571767c5feSopenharmony_ci 581767c5feSopenharmony_ciLast demo update: v8.13.30. 591767c5feSopenharmony_ci 601767c5feSopenharmony_ciNote: Even though the library (main branch/[maven release](https://repo1.maven.org/maven2/com/googlecode/libphonenumber/libphonenumber/8.12.56/)) 611767c5feSopenharmony_ciis at v8.12.57, because of some deployment issues, we were unable to update the 621767c5feSopenharmony_ciJava demo with the new binary version. We will soon fix this. Meantime, please 631767c5feSopenharmony_ciuse JS demo. 641767c5feSopenharmony_ci 651767c5feSopenharmony_ciIf this number is lower than the [latest release's version 661767c5feSopenharmony_cinumber](https://github.com/google/libphonenumber/releases), we are between 671767c5feSopenharmony_cireleases and the demo may be at either version. 681767c5feSopenharmony_ci 691767c5feSopenharmony_ci### Demo App 701767c5feSopenharmony_ci 711767c5feSopenharmony_ciThere is a demo Android App called [E.164 Formatter](java/demoapp) in this 721767c5feSopenharmony_cirepository. The purpose of this App is to show an example of how the library can 731767c5feSopenharmony_cibe used in a real-life situation, in this case specifically in an Android App 741767c5feSopenharmony_ciusing Java. 751767c5feSopenharmony_ci 761767c5feSopenharmony_ci## JavaScript 771767c5feSopenharmony_ci 781767c5feSopenharmony_ciThe [JavaScript 791767c5feSopenharmony_cidemo](https://htmlpreview.github.io/?https://github.com/google/libphonenumber/blob/master/javascript/i18n/phonenumbers/demo-compiled.html) 801767c5feSopenharmony_cimay be run at various tags; this link will take you to `master`. 811767c5feSopenharmony_ci 821767c5feSopenharmony_ci# Java code 831767c5feSopenharmony_ci 841767c5feSopenharmony_ciTo include the Java code in your application, either integrate with Maven (see 851767c5feSopenharmony_ci[wiki](https://github.com/google/libphonenumber/wiki)) or download the latest 861767c5feSopenharmony_cijars from the [Maven 871767c5feSopenharmony_cirepository](https://repo1.maven.org/maven2/com/googlecode/libphonenumber/libphonenumber/). 881767c5feSopenharmony_ci 891767c5feSopenharmony_ci# Javadoc 901767c5feSopenharmony_ci 911767c5feSopenharmony_ciJavadoc is automatically updated to reflect the latest release at 921767c5feSopenharmony_cihttps://javadoc.io/doc/com.googlecode.libphonenumber/libphonenumber/. 931767c5feSopenharmony_ci 941767c5feSopenharmony_ci# Versioning and Announcements 951767c5feSopenharmony_ci 961767c5feSopenharmony_ciWe generally choose the release number following these guidelines. 971767c5feSopenharmony_ci 981767c5feSopenharmony_ciIf any of the changes pushed to master since the last release are incompatible 991767c5feSopenharmony_ciwith the intent / specification of an existing libphonenumber API or may cause 1001767c5feSopenharmony_cilibphonenumber (Java, C++, or JS) clients to have to change their code to keep 1011767c5feSopenharmony_cibuilding, we publish a major release. For example, if the last release were 1021767c5feSopenharmony_ci7.7.3, the new one would be 8.0.0. 1031767c5feSopenharmony_ci 1041767c5feSopenharmony_ciIf any of those changes *enable* clients to update their code to take advantage 1051767c5feSopenharmony_ciof new functionality, and if clients would have to roll-back these changes in 1061767c5feSopenharmony_cithe event that the release was marked as "bad", we publish a minor release. For 1071767c5feSopenharmony_ciexample, we'd go from 7.7.3 to 7.8.0. 1081767c5feSopenharmony_ci 1091767c5feSopenharmony_ciOtherwise, including when a release contains only 1101767c5feSopenharmony_ci[metadata](FAQ.md#metadata_definition) changes, we publish a sub-minor release, 1111767c5feSopenharmony_cie.g. 7.7.3 to 7.7.4. 1121767c5feSopenharmony_ci 1131767c5feSopenharmony_ciSometimes we make internal changes to the code or metadata that, while not 1141767c5feSopenharmony_ciaffecting compatibility for clients, could affect compatibility for **porters** 1151767c5feSopenharmony_ciof the library. For such changes we make announcements to 1161767c5feSopenharmony_ci[libphonenumber-discuss]( 1171767c5feSopenharmony_cihttps://groups.google.com/forum/#!forum/libphonenumber-discuss). Such changes 1181767c5feSopenharmony_ciare not reflected in the version number, and we would publish a sub-minor 1191767c5feSopenharmony_cirelease if there were no other changes. 1201767c5feSopenharmony_ci 1211767c5feSopenharmony_ciWant to get notified of new releases? During most of the year, excepting 1221767c5feSopenharmony_ciholidays and extenuating circumstances, we release fortnightly. We update 1231767c5feSopenharmony_ci[release tags](https://github.com/google/libphonenumber/releases) and 1241767c5feSopenharmony_cidocument detailed [release notes]( 1251767c5feSopenharmony_cihttps://github.com/google/libphonenumber/blob/master/release_notes.txt). 1261767c5feSopenharmony_ciWe also send an announcement to [libphonenumber-discuss]( 1271767c5feSopenharmony_cihttps://groups.google.com/forum/#!forum/libphonenumber-discuss) for every 1281767c5feSopenharmony_cirelease. 1291767c5feSopenharmony_ci 1301767c5feSopenharmony_ci# Quick Examples 1311767c5feSopenharmony_ci 1321767c5feSopenharmony_ciLet's say you have a string representing a phone number from Switzerland. This 1331767c5feSopenharmony_ciis how you parse/normalize it into a `PhoneNumber` object: 1341767c5feSopenharmony_ci 1351767c5feSopenharmony_ci```java 1361767c5feSopenharmony_ciString swissNumberStr = "044 668 18 00"; 1371767c5feSopenharmony_ciPhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance(); 1381767c5feSopenharmony_citry { 1391767c5feSopenharmony_ci PhoneNumber swissNumberProto = phoneUtil.parse(swissNumberStr, "CH"); 1401767c5feSopenharmony_ci} catch (NumberParseException e) { 1411767c5feSopenharmony_ci System.err.println("NumberParseException was thrown: " + e.toString()); 1421767c5feSopenharmony_ci} 1431767c5feSopenharmony_ci``` 1441767c5feSopenharmony_ci 1451767c5feSopenharmony_ciAt this point, `swissNumberProto` contains: 1461767c5feSopenharmony_ci 1471767c5feSopenharmony_ci```json 1481767c5feSopenharmony_ci{ 1491767c5feSopenharmony_ci "country_code": 41, 1501767c5feSopenharmony_ci "national_number": 446681800 1511767c5feSopenharmony_ci} 1521767c5feSopenharmony_ci``` 1531767c5feSopenharmony_ci 1541767c5feSopenharmony_ci`PhoneNumber` is a class that was originally auto-generated from 1551767c5feSopenharmony_ci`phonenumber.proto` with necessary modifications for efficiency. For details on 1561767c5feSopenharmony_cithe meaning of each field, refer to `resources/phonenumber.proto`. 1571767c5feSopenharmony_ci 1581767c5feSopenharmony_ciNow let us validate whether the number is valid: 1591767c5feSopenharmony_ci 1601767c5feSopenharmony_ci```java 1611767c5feSopenharmony_ciboolean isValid = phoneUtil.isValidNumber(swissNumberProto); // returns true 1621767c5feSopenharmony_ci``` 1631767c5feSopenharmony_ci 1641767c5feSopenharmony_ciThere are a few formats supported by the formatting method, as illustrated 1651767c5feSopenharmony_cibelow: 1661767c5feSopenharmony_ci 1671767c5feSopenharmony_ci```java 1681767c5feSopenharmony_ci// Produces "+41 44 668 18 00" 1691767c5feSopenharmony_ciSystem.out.println(phoneUtil.format(swissNumberProto, PhoneNumberFormat.INTERNATIONAL)); 1701767c5feSopenharmony_ci// Produces "044 668 18 00" 1711767c5feSopenharmony_ciSystem.out.println(phoneUtil.format(swissNumberProto, PhoneNumberFormat.NATIONAL)); 1721767c5feSopenharmony_ci// Produces "+41446681800" 1731767c5feSopenharmony_ciSystem.out.println(phoneUtil.format(swissNumberProto, PhoneNumberFormat.E164)); 1741767c5feSopenharmony_ci``` 1751767c5feSopenharmony_ci 1761767c5feSopenharmony_ciYou could also choose to format the number in the way it is dialed from another 1771767c5feSopenharmony_cicountry: 1781767c5feSopenharmony_ci 1791767c5feSopenharmony_ci```java 1801767c5feSopenharmony_ci// Produces "011 41 44 668 1800", the number when it is dialed in the United States. 1811767c5feSopenharmony_ciSystem.out.println(phoneUtil.formatOutOfCountryCallingNumber(swissNumberProto, "US")); 1821767c5feSopenharmony_ci``` 1831767c5feSopenharmony_ci 1841767c5feSopenharmony_ci## Formatting Phone Numbers 'as you type' 1851767c5feSopenharmony_ci 1861767c5feSopenharmony_ci```java 1871767c5feSopenharmony_ciPhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance(); 1881767c5feSopenharmony_ciAsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter("US"); 1891767c5feSopenharmony_ciSystem.out.println(formatter.inputDigit('6')); // Outputs "6" 1901767c5feSopenharmony_ci... // Input more digits 1911767c5feSopenharmony_ciSystem.out.println(formatter.inputDigit('3')); // Now outputs "650 253" 1921767c5feSopenharmony_ci``` 1931767c5feSopenharmony_ci 1941767c5feSopenharmony_ci## Geocoding Phone Numbers 1951767c5feSopenharmony_ci 1961767c5feSopenharmony_ci```java 1971767c5feSopenharmony_ciPhoneNumberOfflineGeocoder geocoder = PhoneNumberOfflineGeocoder.getInstance(); 1981767c5feSopenharmony_ci// Outputs "Zurich" 1991767c5feSopenharmony_ciSystem.out.println(geocoder.getDescriptionForNumber(swissNumberProto, Locale.ENGLISH)); 2001767c5feSopenharmony_ci// Outputs "Zürich" 2011767c5feSopenharmony_ciSystem.out.println(geocoder.getDescriptionForNumber(swissNumberProto, Locale.GERMAN)); 2021767c5feSopenharmony_ci// Outputs "Zurigo" 2031767c5feSopenharmony_ciSystem.out.println(geocoder.getDescriptionForNumber(swissNumberProto, Locale.ITALIAN)); 2041767c5feSopenharmony_ci``` 2051767c5feSopenharmony_ci 2061767c5feSopenharmony_ci## Mapping Phone Numbers to original carriers 2071767c5feSopenharmony_ci 2081767c5feSopenharmony_ciCaveat: We do not provide data about the current carrier of a phone number, only 2091767c5feSopenharmony_cithe original carrier who is assigned the corresponding range. Read about [number 2101767c5feSopenharmony_ciportability](FAQ.md#what-is-mobile-number-portability). 2111767c5feSopenharmony_ci 2121767c5feSopenharmony_ci```java 2131767c5feSopenharmony_ciPhoneNumber swissMobileNumber = 2141767c5feSopenharmony_ci new PhoneNumber().setCountryCode(41).setNationalNumber(798765432L); 2151767c5feSopenharmony_ciPhoneNumberToCarrierMapper carrierMapper = PhoneNumberToCarrierMapper.getInstance(); 2161767c5feSopenharmony_ci// Outputs "Swisscom" 2171767c5feSopenharmony_ciSystem.out.println(carrierMapper.getNameForNumber(swissMobileNumber, Locale.ENGLISH)); 2181767c5feSopenharmony_ci``` 2191767c5feSopenharmony_ci 2201767c5feSopenharmony_ciMore examples on how to use the library can be found in the [unit 2211767c5feSopenharmony_citests](https://github.com/google/libphonenumber/tree/master/java/libphonenumber/test/com/google/i18n/phonenumbers). 2221767c5feSopenharmony_ci 2231767c5feSopenharmony_ci# Third-party Ports 2241767c5feSopenharmony_ci 2251767c5feSopenharmony_ciSeveral third-party ports of the phone number library are known to us. We share 2261767c5feSopenharmony_cithem here in case they're useful for developers. 2271767c5feSopenharmony_ci 2281767c5feSopenharmony_ciHowever, we emphasize that these ports are by developers outside the 2291767c5feSopenharmony_cilibphonenumber project. We do not evaluate their quality or influence their 2301767c5feSopenharmony_cimaintenance processes. 2311767c5feSopenharmony_ci 2321767c5feSopenharmony_ci* C#: https://github.com/twcclegg/libphonenumber-csharp 2331767c5feSopenharmony_ci* Gleam: https://github.com/massivefermion/phony 2341767c5feSopenharmony_ci* Go: https://github.com/nyaruka/phonenumbers 2351767c5feSopenharmony_ci* Objective-c: https://github.com/iziz/libPhoneNumber-iOS 2361767c5feSopenharmony_ci* Swift: https://github.com/marmelroy/PhoneNumberKit 2371767c5feSopenharmony_ci* PHP: https://github.com/giggsey/libphonenumber-for-php 2381767c5feSopenharmony_ci* PostgreSQL in-database types: https://github.com/blm768/pg-libphonenumber 2391767c5feSopenharmony_ci* Python: https://github.com/daviddrysdale/python-phonenumbers 2401767c5feSopenharmony_ci* Ruby: https://github.com/ianks/mini_phone 2411767c5feSopenharmony_ci* Ruby: https://github.com/daddyz/phonelib 2421767c5feSopenharmony_ci* Ruby: https://github.com/mobi/telephone_number 2431767c5feSopenharmony_ci* Rust: https://github.com/1aim/rust-phonenumber 2441767c5feSopenharmony_ci* Erlang: https://github.com/marinakr/libphonenumber_erlang 2451767c5feSopenharmony_ci* Clojure: https://github.com/randomseed-io/phone-number 2461767c5feSopenharmony_ci* R: https://github.com/socialresearchcentre/dialr/ 2471767c5feSopenharmony_ci* Elixir: https://github.com/socialpaymentsbv/ex_phone_number 2481767c5feSopenharmony_ci* Salesforce: https://appexchange.salesforce.com/appxListingDetail?listingId=a0N3A00000G12oJUAR 2491767c5feSopenharmony_ci 2501767c5feSopenharmony_ciAlternatives to our own versions: 2511767c5feSopenharmony_ci 2521767c5feSopenharmony_ci* Android-optimized: Our Java version loads the metadata from 2531767c5feSopenharmony_ci `Class#getResourcesAsStream` and asks that Android apps follow the Android 2541767c5feSopenharmony_ci loading best practices of repackaging the metadata and loading from 2551767c5feSopenharmony_ci `AssetManager#open()` themselves 2561767c5feSopenharmony_ci ([FAQ](https://github.com/google/libphonenumber/blob/master/FAQ.md#optimize-loads)). 2571767c5feSopenharmony_ci If you don't want to do this, check out the port at 2581767c5feSopenharmony_ci https://github.com/MichaelRocks/libphonenumber-android, which does repackage 2591767c5feSopenharmony_ci the metadata and use `AssetManager#open()`, and may be depended on without 2601767c5feSopenharmony_ci needing those specific loading optimizations from clients. You should also check 2611767c5feSopenharmony_ci out the port at https://github.com/lionscribe/libphonenumber-android which also 2621767c5feSopenharmony_ci supports geocoding, and only requires a one line code change. 2631767c5feSopenharmony_ci* Javascript: If you don't want to use our version, which depends on Closure, 2641767c5feSopenharmony_ci there are several other options, including 2651767c5feSopenharmony_ci https://github.com/catamphetamine/libphonenumber-js - a stripped-down 2661767c5feSopenharmony_ci rewrite, about 110 KB in size - and 2671767c5feSopenharmony_ci https://github.com/seegno/google-libphonenumber - a browserify-compatible 2681767c5feSopenharmony_ci wrapper around the original unmodified library installable via npm, which 2691767c5feSopenharmony_ci packs the Google Closure library, about 420 KB in size. 2701767c5feSopenharmony_ci 2711767c5feSopenharmony_ciTools based on libphonenumber metadata: 2721767c5feSopenharmony_ci 2731767c5feSopenharmony_ci* Scala: https://github.com/mr-tolmach/raf - library for generating valid phone numbers in the E.164 format 274