2017 © Pedro Peláez
 

php phone

image

aariess/phone

  • Saturday, July 28, 2018
  • by Aariess
  • Repository
  • 0 Watchers
  • 0 Stars
  • 0 Installations
  • Ruby
  • 0 Dependents
  • 0 Suggesters
  • 126 Forks
  • 0 Open issues
  • 11 Versions
  • 0 % Grown

The README.md

phone

Beta Gem Version Gem Version Build Status Dependency Status Code Climate, (*1)

Parsing, validating and creating phone numbers, (*2)

Version

This documentation is for the unreleased development branch., (*3)

Description

Ruby library for phone number parsing, validation, and formatting., (*4)

Features

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., (*5)

Each country code can have a regular expression named area_code that describes what the area code for that particular country looks like., (*6)

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

Validating

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

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

Formatting

Formating is done via the #format method. The method accepts a Symbol or a String., (*9)

When given a string, it interpolates the string with the following fields:, (*10)

  • %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., (*11)

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:, (*12)

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

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., (*13)

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

require 'phone'

Initializing

Initialize a new phone object with the number, area code, country code and extension number:, (*14)

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

Parsing

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

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:, (*16)

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:, (*17)

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

Same goes for the area code:, (*18)

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

or, (*19)

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

Phoner::Phone.parse '451-588'

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

There's an issue with Germany and Spanish area codes., (*20)

Requirements

Install

$ gem install phone

Or as a Rails plugin, (*21)

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

Copyright (c) 2010-2013 Tomislav Car, Infinum Copyright (c) 2013 Don Morrison, (*22)

See LICENSE.txt for details., (*23)

Contributors

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

The Versions

28/07 2018

dev-code-cleaning

dev-code-cleaning

  Sources   Download

28/07 2018

1.2.x-dev

1.2.9999999.9999999-dev

  Sources   Download

03/03 2018

v1.3.0.beta1

1.3.0.0-beta1

  Sources   Download

03/03 2018

v1.2.3

1.2.3.0

  Sources   Download

03/03 2018

v1.3.0.beta0

1.3.0.0-beta

  Sources   Download

03/03 2018

v1.2.1

1.2.1.0

  Sources   Download

03/03 2018

v1.2.2

1.2.2.0

  Sources   Download

03/03 2018

v1.0

1.0.0.0

  Sources   Download

03/03 2018

v1.1

1.1.0.0

  Sources   Download

03/03 2018

v1.2

1.2.0.0

  Sources   Download

24/02 2017

dev-master

9999999-dev

the first composer package

  Sources   Download

aaa

The Requires

  • php >=5.6.0

 

by Avatar Aariess