giggsey / libphonenumber-for-php Goto Github PK
View Code? Open in Web Editor NEWPHP version of Google's phone number handling library
Home Page: https://giggsey.com/libphonenumber/
License: Apache License 2.0
PHP version of Google's phone number handling library
Home Page: https://giggsey.com/libphonenumber/
License: Apache License 2.0
Looking at: https://github.com/giggsey/libphonenumber-for-php/blob/master/src/libphonenumber/geocoding/data/de/49.php
contains way less data then:
https://github.com/googlei18n/libphonenumber/blob/master/resources/geocoding/de/49.txt
Why is this?
Here is the result in the demo:
http://giggsey.com/libphonenumber/?phonenumber=%2B5493516315733+ext.&country=AR&language=®ion=
as you can see, the input number is +5493516315733 ext.
, and the parsed national number is 93516315733398
Hi,
There is \libphonenumber\PhoneNumber::getCountryCode()
for retrieving country codes, but no such methods for area/carrier code or the number itself (without country and carrier codes). Are there any other ways to accomplish that?
Hello,
I would like to add the 07 prefix for french mobile lines but I don't know which file I have to modify. Can you, please, tell me what I have to do?
Thx.
selosse vianney
The example web app formats a German 0151 mobile phone number the wrong way. It should be +49 151 12345678, but displays as +49 1511 2345678. The other formats are wrong, too.
Link: http://giggsey.com/libphonenumber/?phonenumber=015112345678&country=DE&language=de®ion=DE
Hi..
I got problem when calling getNumberType..
This is my script..
$number = "044 668 18 00";
$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
$number = $phoneUtil->parse($number, "CH");
$gType = $phoneUtil->getNumberType($number);
var_dump($gType);
above script just return int(0)
Hi,
I'm using your library but I have some problems with Argentine phone number:
The next two numbers are valid but the library returns that are invalid.
0351-152-303-473
0351-152-389-491
I've used the online verision of google code library http://libphonenumber.appspot.com/ and it returns in both cases that they are valid
The code I've used is:
$numberStr = "0351-152-303-473";
$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
$numberProto = $phoneUtil->parse($numberStr, "AR");
$isValid = $phoneUtil->isValidNumber($numberProto);
$numberStr = "0351-152-389-491";
$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
$numberProto = $phoneUtil->parse($numberStr, "AR");
$isValid = $phoneUtil->isValidNumber($numberProto);
Thanks in advance!
Lluis
Namespace for this class is libphonenumber
, it's a bit weird. Why not UpperCamelCase like LibPhoneNumber
?
It looks like the stock composer.json in README.md isn't complete enough or isn't correct.
I'm getting the following error when trying to load the library via composer:
Your requirements could not be resolved to an installable set of packages.
Problem 1
- The requested package giggsey/libphonenumber-for-php ~6.0 could not be found.
Potential causes:
- A typo in the package name
- The package is not available in a stable-enough version according to your minimum-stability setting
see <https://groups.google.com/d/topic/composer-dev/_g3ASeIFlrc/discussion> for more details.
I'm not sure what else needs to be added, but I'm guessing something is either wrong or missinge
is there any way to differ USA mobile number from skype number?
For now, it recognize USA mobile number type and skype number type identically - FIXED_LINE_OR_MOBILE = 2
Example number: +6903010
Reported via email
Hello !
I saw that the getAsYouTypeFormatter functionality is missing from your package. :(
It is present it the official Google library (v7.1.0).
Can you please implement it?
I am refering to this:
Formatting Phone Numbers 'as you type'
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter("US");
System.out.println(formatter.inputDigit('6')); // Outputs "6"
... // Input more digits
System.out.println(formatter.inputDigit('3')); // Now outputs "650 253"
isPossibleNumberWithReason returns true for number +420999999999. But isValidNumberForRegion returns false... I'm wondering how to detect invalid number during typing... If user enter "+420999" it should be marked as invalid.... how to do it ?
Thank you very much!
This is a thought I've had for a few weeks now, but writing it down so progress can be tracked.
The ext-intl dependency only exists because the Offline Geocoder needs to use Locale functions to get the country name.
This makes the library dependent on the version of libicu installed on the server, which could be out of date.
For instance, one of my Ubuntu (latest LTS release) machines returns different country names to the Travis boxes, so the unit tests do not pass on my local machine.
Introduce a new package that only contains the functions that the Offline Geocoder requires, and compile the latest data from CLDR. This would be a separate package from libphonenumber-for-php to allow the two to update independently.
$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
$numberProto = $phoneUtil->parse('123456', 'GB');
$region = $phoneUtil->getRegionCodeForNumber($numberProto);
$region is NULL;
Thsi works well for FR or DE...
Notice here $regionMetadata->getCountryCode(), there is no check to make sure that regionMetadata is an object, thus producing a fatal error.
$normalizedNationalNumber .= $this->normalize($nationalNumber);
if ($defaultRegion !== null) {
$countryCode = $regionMetadata->getCountryCode();
$phoneNumber->setCountryCode($countryCode);
} else if ($keepRawInput) {
$phoneNumber->clearCountryCodeSource();
}
}
The reason is on line 1414
$regionMetadata = $this->getMetadataForRegion($defaultRegion);
returns null if its not a valid region.
public function getMetadataForRegion($regionCode)
{
if (!$this->isValidRegionCode($regionCode)) {
return null;
}
if (!isset($this->regionToMetadataMap[$regionCode])) {
// The regionCode here will be valid and won't be '001', so we don't need to worry about
// what to pass in for the country calling code.
$this->loadMetadataFromFile($this->currentFilePrefix, $regionCode, 0, $this->metadataLoader);
}
return isset($this->regionToMetadataMap[$regionCode]) ? $this->regionToMetadataMap[$regionCode] : null;
}
my suggestion is to test for null or throw an exception if the region code is not valid.
Building the geo-data on my modest PC takes at least an hour.
This is a ridiculously long amount of time.
Low priority, just something to look at when time permits.
(If anyone else sees this, and fancies a crack, please give it a go and submit PR's).
$sortNumberUtil = \libphonenumber\ShortNumberInfo::getInstance();
$isEmergencyNumber = $sortNumberUtil->isEmergencyNumber('999', 'GB'); // false
// However...
\libphonenumber\PhoneNumberUtil::getInstance();
$sortNumberUtil = \libphonenumber\ShortNumberInfo::getInstance();
$isEmergencyNumber = $sortNumberUtil->isEmergencyNumber('999', 'GB'); // true
Pull request coming soon
Test number: 447797752305
Default location: GB
Demo at http://libphonenumber.appspot.com/phonenumberparser correctly reports location as "Jersey"
Possibly related to #17 ?
Hello guys. It's not a issue, it's a question, and sorry about this newbie question.
I tried load this class with no success. In the demo I saw "/vendor/autoload.php" and I didin't found it here. I think that have some way to do that and I don't know.
Can you guys help me with that? How can I include in my PHP script?
Thanks.
If supplying an international number to parse, the country code should be optional.
$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
$number = $phoneUtil->parse($phone, "");
Example - http://libphonenumber.appspot.com/, enter a international number, leave step 2 and 3, it parse the number correctly and returns country code etc.
Hi there,
I have a form and I wonder how(if it's possible) can I get the country code corresponding to the given phone number? Because today a belgian guy filled my form with his belgian mobile phone number and it seemed that belgian mobile phones and french fixed lines are alike. I know that in this case it would be hard to get the right country code but before today, only french guys filled my form and soon it will be belgian and swiss people that will fill my form and maybe others in the future, so, Can I get the country code to the given number and How?
Thank you guys!
Is it possible to get the country code from a number eg: get GB from 07400 123456
Running phing inside /lib/3rd/vendor/giggsey/libphonenumber-for-php/ results in a fail to require with build.php.
Buildfile: /{redacted}/lib/3rd/vendor/giggsey/libphonenumber-for-php/build.xml
libphonenumber-for-php > delete-test-carrierdata:
[delete] Deleting directory /{redacted}/lib/3rd/vendor/giggsey/libphonenumber-for-php/Tests/libphonenumber/Tests/carrier/data
[mkdir] Created dir: /{redacted}/lib/3rd/vendor/giggsey/libphonenumber-for-php/Tests/libphonenumber/Tests/carrier/data
libphonenumber-for-php > delete-test-prefixmapper:
[delete] Deleting directory /{redacted}/lib/3rd/vendor/giggsey/libphonenumber-for-php/Tests/libphonenumber/Tests/prefixmapper/data
[mkdir] Created dir: /{redacted}/lib/3rd/vendor/giggsey/libphonenumber-for-php/Tests/libphonenumber/Tests/prefixmapper/data
libphonenumber-for-php > delete-test-coredata:
[delete] Deleting directory /{redacted}/lib/3rd/vendor/giggsey/libphonenumber-for-php/Tests/libphonenumber/Tests/core/data
[mkdir] Created dir: /{redacted}/lib/3rd/vendor/giggsey/libphonenumber-for-php/Tests/libphonenumber/Tests/core/data
libphonenumber-for-php > delete-test-timezone:
[delete] Deleting directory /{redacted}/lib/3rd/vendor/giggsey/libphonenumber-for-php/Tests/libphonenumber/Tests/timezone/data
[mkdir] Created dir: /{redacted}/lib/3rd/vendor/giggsey/libphonenumber-for-php/Tests/libphonenumber/Tests/timezone/data
libphonenumber-for-php > cleanup-test-data:
libphonenumber-for-php > svn-checkout:
Checked out revision 712.
libphonenumber-for-php > build-test-metadata:
PHP Warning: require(/{redacted}/lib/3rd/vendor/giggsey/libphonenumber-for-php/build/../vendor/autoload.php): failed to open stream: No such file or directory in /{redacted}/lib/3rd/vendor/giggsey/libphonenumber-for-php/build/build.php on line 4
PHP Fatal error: require(): Failed opening required '/{redacted}/lib/3rd/vendor/giggsey/libphonenumber-for-php/build/../vendor/autoload.php' (include_path='.:/usr/share/php:/usr/share/pear') in /{redacted}/lib/3rd/vendor/giggsey/libphonenumber-for-php/build/build.php on line 4
libphonenumber-for-php > build-geo-test-data:
PHP Warning: require(/{redacted}/lib/3rd/vendor/giggsey/libphonenumber-for-php/build/../vendor/autoload.php): failed to open stream: No such file or directory in /{redacted}/lib/3rd/vendor/giggsey/libphonenumber-for-php/build/build.php on line 4
PHP Fatal error: require(): Failed opening required '/{redacted}/lib/3rd/vendor/giggsey/libphonenumber-for-php/build/../vendor/autoload.php' (include_path='.:/usr/share/php:/usr/share/pear') in /{redacted}/lib/3rd/vendor/giggsey/libphonenumber-for-php/build/build.php on line 4
libphonenumber-for-php > build-carrier-test-data:
PHP Warning: require(/{redacted}/lib/3rd/vendor/giggsey/libphonenumber-for-php/build/../vendor/autoload.php): failed to open stream: No such file or directory in /{redacted}/lib/3rd/vendor/giggsey/libphonenumber-for-php/build/build.php on line 4
PHP Fatal error: require(): Failed opening required '/{redacted}/lib/3rd/vendor/giggsey/libphonenumber-for-php/build/../vendor/autoload.php' (include_path='.:/usr/share/php:/usr/share/pear') in /{redacted}/lib/3rd/vendor/giggsey/libphonenumber-for-php/build/build.php on line 4
libphonenumber-for-php > build-timezones-test-data:
PHP Warning: require(/{redacted}/lib/3rd/vendor/giggsey/libphonenumber-for-php/build/../vendor/autoload.php): failed to open stream: No such file or directory in /{redacted}/lib/3rd/vendor/giggsey/libphonenumber-for-php/build/build.php on line 4
PHP Fatal error: require(): Failed opening required '/{redacted}/lib/3rd/vendor/giggsey/libphonenumber-for-php/build/../vendor/autoload.php' (include_path='.:/usr/share/php:/usr/share/pear') in /{redacted}/lib/3rd/vendor/giggsey/libphonenumber-for-php/build/build.php on line 4
libphonenumber-for-php > compile-test-data:
libphonenumber-for-php > test:
sh: vendor/bin/phpunit: No such file or directory
BUILD FINISHED
Total time: 1.4527 second
I tried modifying build.php to include the vender/autoload.php but that just resulted in:
PHP Fatal error: Class 'libphonenumber\buildtools\BuildApplication' not found in /{redacted}/lib/3rd/vendor/giggsey/libphonenumber-for-php/build/build.php on line 6
Good day,
I'm using your library and also I'm using the JavaScript version provided by Google.
After updating to the latest version that you provided based on the Google 5.9 update, I came across a "KW" phone number that validates in Google's Java and Javascript code but fails in the PHP implementation.
After reviewing both codes, I noticed a missing line between the 2 regex codes.
5.9 version uses this pattern:
'NationalNumberPattern' => '
(?:
5(?:
1[0-5]|
[05]\d
)|
5(?:
5[015-9]
)|
6(?:
0[034679]|
5[015-9]|
6\d|
7[067]|
9[0369]
)|
9(?:
0[09]|
4[049]|
6[069]|
[79]\d|
8[08]
)
)\d{5}
',
while the lib code is missing the pattern which I added later
5(?:
5[015-9]
)|
Anyways, I figured that I let you know about it,
and in appreciation to your efforts, if you need any help, please consider my request to become one of the contributors.
regards
Hi there
I'm trying to work out how to get an international number using the country code instead of the country name - e.g. '44' instead of 'GB'. Obviously the simple format is '+'.$countryCode+phoneNumber
, but seeing as only some countries drop the preceding zero, a simple rule like that wouldn't work (I don't think).
$countryCode = '44';
$phone = '01234567899';
$internationalNumber = '+441234567899';
Is there a way to do this with this library? essentially I just want to send '44' to the parse()
function instead of 'GB';
Thanks!
The current version of the PHP returns +[Country Code][National Number]
.
However, this is misleading, as it looks like it's E164. However, for some regions/types of number, this is not valid. For example, the example number of Italy is +390212345678
, but if you parse this number, then do a __toString()
on it, it'll actually return +39212345678
(note the missing 0 after the country code).
The Java version of the project does the following:
public String toString() {
StringBuilder outputString = new StringBuilder();
outputString.append("Country Code: ").append(countryCode_);
outputString.append(" National Number: ").append(nationalNumber_);
if (hasItalianLeadingZero() && isItalianLeadingZero()) {
outputString.append(" Leading Zero(s): true");
}
if (hasNumberOfLeadingZeros()) {
outputString.append(" Number of leading zeros: ").append(numberOfLeadingZeros_);
}
if (hasExtension()) {
outputString.append(" Extension: ").append(extension_);
}
if (hasCountryCodeSource()) {
outputString.append(" Country Code Source: ").append(countryCodeSource_);
}
if (hasPreferredDomesticCarrierCode()) {
outputString.append(" Preferred Domestic Carrier Code: ").
append(preferredDomesticCarrierCode_);
}
return outputString.toString();
}
For the example Italian number, this causes the output to be:
Country Code: 39 National Number: 212345678 Leading Zero(s): true
My question to people who see this is: Should we change the string output to match what the Java version has. The issue has been present since 6fec96d.
From memory, this is an issue with the Regex used.
Need to write a simple testable script with a failing number
First - thanks for putting this together it works brilliantly.
Unfortunately I have found a bit of an edge case that seems to break things. If I understand correctly Bolton & Brampton are both UK counties that have legitimate 10 & 11 digit phone numbers.
(01204) xxxxx Bolton
(016977) xxxx Brampton
You can see a list of valid Bolton phone numbers here - http://phonenumbers.nr5.eu/prefix/01204/number/0120410120
Example number: +447624806000
Google says: Isle Of Man
libphonenumber-for-php says: United States
This looks to be because of the list of Locales we have.
http://itc.ua/news/operatoru-life-vyidali-tretiy-mobilnyiy-kod-073/
Please make +38073 a valid code in Ukraine.
For now valid phone +380733108920 cosidered as invalid.
In case you hadn't already tracked it down, the failing tests on PHP7 appear to be related to be a bug: https://bugs.php.net/bug.php?id=70110
Hmm, it is strange, but Indian is missing from list ;(
+91
Hi,
while trying to parse a phone number from Colombia, I've seen that the lib returns that this phone is valid: 573133333333
(57 is the country code for CO)
http://giggsey.com/libphonenumber/?phonenumber=573133333333&country=CO&language=®ion=
But when trying to parse this number: 00573133333333 (this is the same number returned by the API for calling from GB) the method isValidNumber returns false.
http://giggsey.com/libphonenumber/?phonenumber=00573133333333&country=CO&language=®ion=
This does not happens when validating other countries, which accepts the full country code, so I could think this is a possible issue.
Thanks,
Oriol
Hello everybody. I get some problems with en/86.php. This file is too big compare with others (4,8Mb).
So, I must spend big amount of valuable memory (almost 75Mb) when use geocoding for Chinese phone numbers. When I add necessary size of memory by ini_set (although it is not good) I anyway get problems with geocoding some numbers. For example I try use geocoding for 86-157-9662-1289.
Actualy there is no such number and I should get empty result, instead I get fatal error, because it is need even more memory. Why that, I don't no. Anybody knows how to resolve this problem with memory wasting? May be should split the file 86.php into smaller files. Please, help me.
version
"giggsey/libphonenumber-for-php":"7.0.2"
http://giggsey.com/libphonenumber/?phonenumber=%2B373+68+111+111&country=MD&language=®ion=MD
show (it's mobile type)
+373 681 11 111
must be formatted as: +373 68 111 111
For local (landline) phonenumber - all good
http://giggsey.com/libphonenumber/?phonenumber=%2B373%2022%20111%20111&country=MD&language=®ion=MD
you can see formats in wiki
https://en.wikipedia.org/wiki/Telephone_numbers_in_Moldova
0-xx-xx-xx-xx call between localities and mobile phone companies
+373-xx-xx-xx-xx call to Moldova from abroad
similar discussion https://github.com/googlei18n/libphonenumber/issues/632
The landline number of Guwahati, India was updated to 8 digits from 7 by prepending a 2 since 2002. This change is not reflected in the Indian phone metadata file.
The number is 0361-22300102, which is show as false.
Please update the regex to match this pattern.
Ref: See section "9-10 November 2002 - Assam area changes" in http://www.wtng.info/wtng-91-in.html for more details.
Fatal error appears here:
https://github.com/giggsey/libphonenumber-for-php/blob/master/src/libphonenumber/ShortNumberInfo.php#L311 in $phoneMetadata->getGeneralDesc()
when $phoneMetadata
is null.
It happens if region is not found in ShortNumbersRegionCodeSet::$shortNumbersRegionCodeSet
. For example the number 322 for region NE (Niger)
Is it possible to display a number with a leading zero? So similar to international, but with with (0) added.
+31 123 456 789 --> +31 (0) 123 456 789
Please consider releasing the full code used on http://giggsey.com/libphonenumber/ to illustrate an end-to-end demo. Also please mention use of composer, as this isn't immediately obvious to everyone.
Excellent work on the lib though, it works like a charm!
I am using this API and have downloaded the whole project through composer.
So whenever the any git file is updated would i'll be notified ?
If not, how would i'll updating my API with the new number series.
Some numbers are converted to a float with E notation, e.g.
$num = $phoneUtil->parse('0358112345678987', 'DE');
echo $phoneUtil->format($num, \libphonenumber\PhoneNumberFormat::E164);
// output:
'+493.5811234567899E+14'
I noticed a cast to float in PhoneNumberUtil.php. If it is removed, the correctly formatted number is returned.
--- PhoneNumberUtil.php.orig 2014-03-12 22:43:02.000000000 +0100
+++ PhoneNumberUtil.php 2014-03-12 22:43:50.000000000 +0100
@@ -1403,7 +1403,7 @@
"The string supplied is too long to be a phone number.");
}
$this->setItalianLeadingZerosForPhoneNumber($normalizedNationalNumber, $phoneNumber);
- $phoneNumber->setNationalNumber((float)$normalizedNationalNumber);
+ $phoneNumber->setNationalNumber($normalizedNationalNumber);
}
/**
Hi~
When i use this library to validate this number "18612249027ab" with region "CN". It seems that this number is a valid mobile number.But exactly it is a wrong mobile number, Chinese mobile does't contain letters.
ps: I am so sorry about my poor english : )
Best regards
Thanks very much
vendor/giggsey/libphonenumber-for-php/build.xml:98:33: You must specify readable directory as repository.
vendor/giggsey/libphonenumber-for-php/vendor/phing/phing/classes/phing/tasks/ext/git/GitFetchTask.php(111): GitBaseTask->getGitClient(false, 'libphonenumber-...')
Happens when I try to run vendor/bin/phing compile.
Hi Joshua,
first of all: thanks for your great work!
I found a problem with libphonenumber-for-php. An e-mail address like [email protected] is converted to +498114686245266 which is a valid phone number for Germany.
https://libphonenumber.appspot.com/ reports an error for [email protected].
best regards
Daniel
Hi,
First thank you for this nice port from the original JS library to PHP.
A great thing in the original version was the ability to process a phone number without knowing the region. I'm quite new to the PHP version, but as far as I understood, it aims at reproducing the behavior of JS version.
In the JS version (demo here), PhoneNumberUtil.parseAndKeepRawInput(phoneNumber, regionCode) can be invoked with a missing/empty regionCode. In this PHP port, it terminates with an exception "Missing or invalid default region".
I browsed the code a bit and found that PhoneNumberUtil::parseAndKeepRawInput() calls PhoneNumberUtil::parseHelper() with $checkRegion parameter set to true (line 1330). Changing this to false fixes the exception, but then the library still isn't able to process the number. For instance, +33655442211 is properly identified as French mobile number by the JS version, but not processed by the PHP version.
I assume it's something not yet implemented, but just in case it's a bug I thought I'd post about it here.
Support for PhoneNumberOfflineGeocoder is needed
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.