GithubHelp home page GithubHelp logo

blobfolio / blob-phone Goto Github PK

View Code? Open in Web Editor NEW
5.0 6.0 0.0 1.55 MB

A light(er)weight implementation of Google's amazing libphonenumber for PHP.

License: Do What The F*ck You Want To Public License

JavaScript 10.21% PHP 81.36% Shell 8.44%
phone-number phone-formatting php libphonenumber international-telephone javascript

blob-phone's Introduction

blob-phone

blob-phone is a light(er)-weight implementation of Google's amazing libphonenumber for PHP.

Build Status

 

Table of Contents

 

Features

blob-phone is primarily concerned with validating and formatting arbitrary telephone numbers in international format (e.g. +1 201-555-0123). Along the way it also manages to gather information about a number's:

  • Country
  • Continent
  • Type (e.g. mobile, voip, etc.)

Other data and formatting features from libphonenumber have been stripped out to reduce the library's size. For a more comprehensive implementation of libphonenumber written for PHP, take a look at libphonenumber-for-php.

 

Installation

The PHP library for blob-phone requires:

Install with Composer:

composer require "blobfolio/blob-phone:dev-master"

 

Use

Get started

$phone = new blobfolio\phone\phone($phone_number, $country_code='US');

The constructor accepts two arguments:

  • (string) Phone number (pre-formatted or not);
  • (string) (optional) ISO country code (suspected); Default: "US"

Providing a probable country of origin will help with identification, so is recommended. Otherwise your number may end up on the wrong side of the planet.

blob-phone uses a brute-force technique to identify and format a phone number, prioritizing tests as follows:

  • The country provided;
  • Other countries with the same prefix;
  • Other countries on the same continent;
  • Everywhere else, alphabetically;

 

Check it

if ($phone->is_phone($types=array())) {
    ...
}

This method accepts an optional array of types of phone numbers in case you are looking for something specific. Type detection is hit or miss, so if you require high accuracy, you should verify this some other way.

The possible types are:

  • "fixed", i.e. landline
  • "mobile"
  • "pager"
  • "personal_number"
  • "premium_rate", e.g. your favorite party line
  • "shared_cost"
  • "toll_free"
  • "voicemail"
  • "voip", e.g. Google Voice

This method returns TRUE or FALSE.

 

Pull It

// If you only want the number, you can typecast it:
$formatted = (string) $phone; //+598 9423 1234

// If you want more details:
print_r($phone->get_data());
/*
Array(
    [country] => CA             
    [prefix] => 1               
    [region] => North America   
    [types] => Array(
        [0] => fixed              
        [1] => mobile
    )
    [number] => +1 204-234-5678
)
*/

 

All Together Now...

$phone = '1012345678';
$country = 'CN';

$phone = (string) blobfolio\phone\phone($phone, $country);
if (strlen($phone)) {
    echo "Your pretty phone number is: $phone";
}
else {
    echo "I don't think that was right...";
}

 

Javascript

blob-phone is also available as a dependency-free Javascript library. To use it, simply include lib/js/blob-phone.min.js in your project.

This library has a single method: blobPhone.parse(), which takes two arguments:

  • (string) Phone number (pre-formatted or not);
  • (string) (optional) ISO country code (suspected); Default: "US"

Providing a probable country of origin will help with identification, so is recommended. Otherwise your number may end up on the wrong side of the planet.

This will return FALSE if the number could not be parsed, or an object containing the number details, like:

var parsed = blobPhone.parse(2015550123);
/*
{
    country: "US",
    number: "+1 201-555-0123",
    prefix: 1,
    region: "North America",
    types: [
        "fixed",
        "mobile"
    ]
}
*/

 

License

Copyright © 2018 Blobfolio, LLC <[email protected]>

This work is free. You can redistribute it and/or modify it under the terms of the Do What The Fuck You Want To Public License, Version 2.

DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004

Copyright (C) 2004 Sam Hocevar <[email protected]>

Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.

DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

0. You just DO WHAT THE FUCK YOU WANT TO.

Donations

Bitcoin QR If you have found this work useful and would like to contribute financially, Bitcoin tips are always welcome!

1Af56Nxauv8M1ChyQxtBe1yvdp2jtaB1GF

blob-phone's People

Contributors

joshstoik1 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.