Lawrence Cooke
Lawrence Cooke

Reputation: 1597

libphonenumber php usage

I am looking at using libphonenumber to convert numbers into friendly local phone numbers

eg. if 19311234567 is entered it returns (931) 123 4567

I found a php version of libphonenumber here: https://github.com/davideme/libphonenumber-for-PHP but I cannot figure out how to use it to get what I am after.

So I am wondering if anyone knows what calls I need to make to it in order to get the phone number converted

Upvotes: 3

Views: 4761

Answers (2)

Azhar Nawaz
Azhar Nawaz

Reputation: 91

I have developed API Services which is actually wrapper of libphonenumber.

You can get National Format of Phone Number:

curl http://phonenumber.ones-app.com/national-format?number=19311234567

(931) 123-4567

For complete Help: http://blog.ones-app.com/ones-phone-number-api/

Upvotes: 1

Davide Mendolia
Davide Mendolia

Reputation: 69

With the actual implementation you can format with the following code.

$phoneNumberUtil = PhoneNumberUtil::getInstance(); 
$exampleNumber = new PhoneNumber();
$exampleNumber->setCountryCode(1)->setNationalNumber(9311234567);
var_dump($phoneNumberUtil->format($exampleNumber, PhoneNumberFormat::INTERNATIONAL));
var_dump($phoneNumberUtil->format($exampleNumber, PhoneNumberFormat::NATIONAL));
var_dump($phoneNumberUtil->format($exampleNumber, PhoneNumberFormat::RFC3966));

Output:

string(15) "+1 931 123 4567"
string(12) "931 123 4567"
string(15) "+1-931-123-4567"

The rawInput and free format is not yet implemented in PHP version, but the API will be similar to the JAVA one.

Upvotes: 2

Related Questions