PHPackages                             aariess/phone - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. aariess/phone

ActivePhp

aariess/phone
=============

the first composer package

08Ruby

Since Feb 24Pushed 9y agoCompare

[ Source](https://github.com/aariess/phone)[ Packagist](https://packagist.org/packages/aariess/phone)[ RSS](/packages/aariess-phone/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

phone
=====

[](#phone)

[![Beta Gem Version](https://camo.githubusercontent.com/0f64fc48600f83301d69881a0bf8ce9d281407b5a3108b3e9b783fc4768b4532/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f626574612d76312e332e302e62657461312d626c75652e737667)](https://rubygems.org/gems/phone/versions/1.3.0.beta1)[![Gem Version](https://camo.githubusercontent.com/f9ba441fe938d507ceb2e4f0e233b1f382008d37251954cc638e4906802e3764/68747470733a2f2f696d672e736869656c64732e696f2f67656d2f762f70686f6e652e737667)](https://rubygems.org/gems/phone)[![Build Status](https://camo.githubusercontent.com/c1aeb50ce154b949848a5b8a066126e9d03ee89bc2f23f56bcb9a726ca6b9ccf/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f636172722f70686f6e652e737667)](https://travis-ci.org/carr/phone)[![Dependency Status](https://camo.githubusercontent.com/0f921896f495a8a3de8600ac2d92237151eab018f2cbfd8130998d4e86a8a066/68747470733a2f2f696d672e736869656c64732e696f2f67656d6e617369756d2f636172722f70686f6e652e737667)](https://gemnasium.com/carr/phone)[![Code Climate](https://camo.githubusercontent.com/9e21cb7a4c86f310061824b3bd9f4b7a7468a0a067807c0edf3d3dc2845e2dac/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636c696d6174652f6769746875622f636172722f70686f6e652e737667)](https://codeclimate.com/github/carr/phone)

Parsing, validating and creating phone numbers

- [Homepage](https://github.com/carr/phone#readme)
- [Issues](https://github.com/carr/phone/issues)
- [Documentation](http://rubydoc.info/gems/phone/frames)
- \[Email\](mailto:don at elskwid.net)

Version
-------

[](#version)

This documentation is for the unreleased development branch.

- Current release: [1.3.0.beta1](https://github.com/carr/phone/tree/v1.3.0.beta1/)
- Current stable: [1.2.3](https://github.com/carr/phone/tree/v1.2.3)

Description
-----------

[](#description)

Ruby library for phone number parsing, validation, and formatting.

Features
--------

[](#features)

### Automatic country and area code detection

[](#automatic-country-and-area-code-detection)

Phone does its best to automatically detect the country and area code while parsing. To do this, phone uses data stored in `data/phone/countries.yml`.

Each country code can have a regular expression named `area_code` that describes what the area code for that particular country looks like.

If an `area_code` regular expression isn't specified, the default, `Phoner::Phone::DEFAULT_AREA_CODE` (correct for the US) is used.

### Validating

[](#validating)

Validating is very relaxed, basically it strips out everything that's not a number or '+' character:

```
Phoner::Phone.valid? 'blabla 091/512-5486 blabla'
```

### Formatting

[](#formatting)

Formating is done via the `#format` method. The method accepts a `Symbol` or a `String`.

When given a string, it interpolates the string with the following fields:

- %c - country\_code (385)
- %a - area\_code (91)
- %A - area\_code with leading zero (091)
- %n - number (5125486)
- %f - first @@n1\_length characters of number (configured through Phoner::Phone.n1\_length), default is 3 (512)
- %l - last characters of number (5486)
- %x - the extension number

```
pn = Phoner::Phone.parse('+385915125486')
pn.to_s # => "+385915125486"
pn.format("%A/%f-%l") # => "091/512-5486"
pn.format("+ %c (%a) %n") # => "+ 385 (91) 5125486"
```

When given a symbol it is used as a lookup for the format in the Phoner::Phone.named\_formats hash.

```
pn.format(:europe) # => "+385 (0) 91 512 5486"
pn.format(:us) # => "(234) 123-4567"
pn.format(:default_with_extension) # => "+3851234567x143"
```

You can add your own custom named formats like so:

```
Phoner::Phone.named_formats[:short] = '%A/%n1-%n2'
pn.format(:short) # => 091/512-5486
```

### Finding countries by their isocode

[](#finding-countries-by-their-isocode)

If you don't have the country code, but you know from other sources what country a phone is from, you can retrieve the country using the country isocode (such as 'de', 'es', 'us', ...). Remember to call `Phoner::Country.load` before using this lookup.

```
if country = Phoner::Country.find_by_country_isocode(user_country_isocode)
  phone_number = Phoner::Phone.parse(user_input, :country_code => country.country_code)
end
```

Examples
--------

[](#examples)

```
require 'phone'
```

### Initializing

[](#initializing)

Initialize a new phone object with the number, area code, country code and extension number:

```
Phoner::Phone.new('5125486', '91', '385')
```

```
Phoner::Phone.new(:number => '5125486', :area_code => '91', :country_code => '385', :extension => '143')
```

### Parsing

[](#parsing)

Create a new phone object by parsing from a string. Phoner::Phone does it's best to detect the country and area codes:

```
Phoner::Phone.parse '+385915125486'
Phoner::Phone.parse '00385915125486'
```

If the country or area code isn't given in the string, you must set it, otherwise it doesn't work:

```
Phoner::Phone.parse '091/512-5486', :country_code => '385'
Phoner::Phone.parse '(091) 512 5486', :country_code => '385'
```

If you feel that it's tedious, set the default country code once:

```
Phoner::Phone.default_country_code = '385'
Phoner::Phone.parse '091/512-5486'
Phoner::Phone.parse '(091) 512 5486'
```

Same goes for the area code:

```
Phoner::Phone.parse '451-588', :country_code => '385', :area_code => '47'
```

or

```
Phoner::Phone.default_country_code = '385'
Phoner::Phone.default_area_code = '47'

Phoner::Phone.parse '451-588'
```

Test Countries
--------------

[](#test-countries)

- \[AU\] Australia
- \[BA\] Bosnia and Herzegovina
- \[BE\] Belgium
- \[DE\] Germany
- \[ES\] Spain
- \[FR\] France
- \[GB\] United Kingdom
- \[HR\] Croatia
- \[HU\] Hungary
- \[IE\] Ireland
- \[ME\] Montenegro
- \[NL\] Netherlands
- \[NZ\] New Zealand
- \[PT\] Portugal
- \[RS\] Serbia
- \[SE\] Sweden
- \[SI\] Slovenia
- \[UA\] Ukraine
- \[US\] United States
- \[UY\] Uruguay
- \[ZA\] South Africa

Known issues
------------

[](#known-issues)

There's an issue with Germany and Spanish area codes.

Requirements
------------

[](#requirements)

Install
-------

[](#install)

```
$ gem install phone

```

Or as a Rails plugin

```
$ script/plugin install git://github.com/carr/phone.git

```

Copyright
---------

[](#copyright)

Copyright (c) 2010-2013 Tomislav Car, [Infinum](http://www.infinum.co)Copyright (c) 2013 Don Morrison

See [LICENSE.txt](LICENSE.txt) for details.

Contributors
------------

[](#contributors)

Don Morrison, Michael Squires, Todd Eichel (Fooala, Inc.), chipiga, Etienne Samson, Luke Randall, Jakob Hilden, Tieg Zaharia

###  Health Score

21

—

LowBetter than 19% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 62.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/12f53006d3a0cabf811e71088133bc46824957788963dbbb5f6d59685573d163?d=identicon)[Aariess](/maintainers/Aariess)

---

Top Contributors

[![elskwid](https://avatars.githubusercontent.com/u/9002?v=4)](https://github.com/elskwid "elskwid (89 commits)")[![carr](https://avatars.githubusercontent.com/u/97653?v=4)](https://github.com/carr "carr (34 commits)")[![chipiga](https://avatars.githubusercontent.com/u/77704?v=4)](https://github.com/chipiga "chipiga (5 commits)")[![tfe](https://avatars.githubusercontent.com/u/21501?v=4)](https://github.com/tfe "tfe (4 commits)")[![bsboris](https://avatars.githubusercontent.com/u/591230?v=4)](https://github.com/bsboris "bsboris (3 commits)")[![tiegz](https://avatars.githubusercontent.com/u/5054?v=4)](https://github.com/tiegz "tiegz (2 commits)")[![webcracy](https://avatars.githubusercontent.com/u/28333?v=4)](https://github.com/webcracy "webcracy (1 commits)")[![lukerandall](https://avatars.githubusercontent.com/u/878?v=4)](https://github.com/lukerandall "lukerandall (1 commits)")[![miloshadzic](https://avatars.githubusercontent.com/u/93555?v=4)](https://github.com/miloshadzic "miloshadzic (1 commits)")[![soulcutter](https://avatars.githubusercontent.com/u/97257?v=4)](https://github.com/soulcutter "soulcutter (1 commits)")[![tiennou](https://avatars.githubusercontent.com/u/81099?v=4)](https://github.com/tiennou "tiennou (1 commits)")

### Embed Badge

![Health badge](/badges/aariess-phone/health.svg)

```
[![Health](https://phpackages.com/badges/aariess-phone/health.svg)](https://phpackages.com/packages/aariess-phone)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
