3

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

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
Lawrence Cooke
  • 1,567
  • 3
  • 26
  • 52
  • 2
    did you ever get this library to work right? I have been having troubles getting it going myself. – ackerchez Aug 10 '12 at 20:58
  • 1
    FYI : This library not support some countries, http://phpinterviewquestions.co.in/blog/ionic/googles-libphonenumber-library-supported-country-list – sijo vijayan Jul 22 '17 at 08:24

2 Answers2

2

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.

burki
  • 3
  • 1
  • Thank you for your reply, I am still having difficulty with this, I think it is due to the fact that I am trying to get it to work in a flat PHP setup and not an object based one? When I copied your code in, after including phonenumberutil,regioncode,phonenumber,countrycodetoregioncodemap, I get the error "Using $this when not an object context" Do you happen to have a full working example, I looked at the Test suite, but it doesnt seem to work either, that one gives an error "PHPUnti_Framework_Testcase not found" So I am little stumped. Your help is greatly appreciated. – Lawrence Cooke Mar 13 '12 at 01:49
  • You need to have PHPUnit setup to run the tests. Can you send me one of your test case and I will debug it. – Davide Mendolia Mar 16 '12 at 11:11
  • ` namespace com\google\i18n\phonenumbers; require_once '../PhoneNumberUtil.php'; require_once '../RegionCode.php'; require_once '../PhoneNumber.php'; require_once '../CountryCodeToRegionCodeMapForTesting.php'; $exampleNumber = new PhoneNumber(); $exampleNumber->setCountryCode(1)->setNationalNumber(9311234567); var_dump($this->phoneUtil->format($exampleNumber, PhoneNumberFormat::INTERNATIONAL)); var_dump($this->phoneUtil->format($exampleNumber, PhoneNumberFormat::NATIONAL)); var_dump($this->phoneUtil->format($exampleNumber, PhoneNumberFormat::RFC3966)); ?>` – Lawrence Cooke Mar 17 '12 at 21:41
  • Thanks davide, as you will see in the code above, the issue I am having is $this doesnt work in this context, and I am unsure how I should be accessing it. – Lawrence Cooke Mar 17 '12 at 21:48
  • OK, sorry, my example was executed inside the test class, you need to use PhoneNumberUtil::getInstance() instead of $this->phoneUtil – Davide Mendolia Mar 24 '12 at 16:13
  • i have tried to use the example there "as is" and it throws a 500 server error. I have also read that using namespaces across domains is seriously not cool juju. That being said I am still interested in using this but maybe I dont understand how the namespace stuff is working. that first "use com\google\i18n\phonenumbers\PhoneNumberUtil;" seems to be causing me problems. Any thoughts on how to make this work? – ackerchez Aug 10 '12 at 20:54
1

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/

Azhar Nawaz
  • 91
  • 12