phone number prefix - phone-number

How would I take a phone number like: 15556667777 and some how get its country code (prefix, the 1).. But, the issue is.. I will have to be able to get ANY numbers country prefix from any phone number. So whether it be UK, AU, US, any INTL country.

It seems like a pretty complicated parsing, with lots of exceptions. You should try some libraries for this purpose:
Google's library:
http://code.google.com/p/libphonenumber/
(Java and JavaScript versions)
There's a python port:
http://pypi.python.org/pypi/phonenumbers/3.3a1

Related

Convert ID from speech to text without spaces

I'm using Google Cloud Speech API with IBM Voice Gateway in order to interact with a VoiceBot through a phone.
If I say an identifier contening letters and numbers through the phone, the Google Cloud Speech converts it into string with spaces. For example, if I say "A1B2C3", it will convert it into the following string "a 1 b 2 c 3".
Do you know if there is way to avoid these useless spaces ?
Thanks for your help!
Lucas
I don't see any way in which you can eliminate spaces from the API response. What you could do is experiment with the available features, as this is probably your best chance to get a recognition more similar to what you are looking for.
For example: you can provide some sample hint phrases echoing your use case, indicate that the audio is a phone call, or use an enhanced model (although for the latter to be available you need to first opt in for data logging).
Honestly though, for your case, it might be better if you post process the returned string (e.g. with a simple "a 1 b 2 c 3".replace(' ','') ).

Twilio - is there a place to find out formatting requirements?

I have been all through the Twilio API docs, and I can't seem to find what requirements they have for certain inputs. For instance, I found that if I am searching for a number in a city, the city can't have any punctuation in it, but sometimes the abbreviated name is required. For instance, searching for in_locality="Ft. Worth" and in_locality="Ft Worth" won't work, but in_locality="Fort Worth" does. Oddly though, other abbreviations are somewhat required, like in_locality="St George" works, but in_locality="Saint George" does not, nor does in_locality="St. George"
Are there any rules for this written down anywhere, or do I just have to figure out every permutation of an abbreviated city name by magic, and try them all?

phone number format mask in ios

I'd like a regex or some function to standardize all phone number input from my ios app.
Thus inputs like the following
(212)555-5555
2125555555
212-555-5555
212 555 5555
212-5555555
all get translated to
(212) 555-5555
would this following regex work to match the phone numbers into 3 match groups which I can then format into the correct output string?
^\D?(\d{3})\D?\D?(\d{3})\D?(\d{4})$
Is the $ sign at the end of the regex required or does that mess things up?
Is regex the best way to do this in iOS?
^ and $ in a regex match the beginning and end of a line of input. You have not provided enough information to determine if these anchors are appropriate in this particular case.
Since you're working in iOS have you looked at the NSDataDetector class? It provides mechanisms for detecting strings which could be valid phone numbers in many different formats. This would give you phone number detection matching the behavior users see in many of the other apps on their devices.
NSDataDetector does not provide a mechanism for re-formatting phone numbers so you would still need to determine how you want to reformat strings detected as possible phone numbers (which may contain more or less than 10 digits). If you do so you should probably fall back to preserving the original format of any detected number which does not match one of your expected formats.

localising postal / physical address display from database fields

Can anyone point me to a list of international postal / residential / delivery address format templates that use some kind of parseable standard vocabulary for address parts?
The ideal list contains a country code then a format using replaceable tokens so I can substitute database address fields into a template to produce something printable in the local format.
for example
NZ | [first_name] [family_name]\n[company_name]\n[street_address]\n[city] [post_code]\n[country]
AU | [first_name] [family_name]\n[company_name]\n[street_address]\n[city]\n[state] [post_code]\n[country]
US | etc
UK | etc
Background: I used to have a simple freetext field to accept addresses. Moving to support vCard download, which requires addresses to be broken down into specific fields. Thats all fine: we can do the migration. I'm looking for a way to display the fields in the "correct" order for each country. thanks for your help!
This MSDN page has the information in the format you need and seems accurate, but covers only 33 countries. Maybe they are enough.
The Universal Postal Union offers all the information you need for a lot of countries here. This is top quality information; however, it is split across as many PDF documents as there are countries and is not in the format you need.
This page provides the information in a slightly more accessible form. As far as I can judge, it is accurate (and contains a lot of valuable info), but I can't speak to its quality nor its currentness.
Google have a JSON-based API that they use for their Android address input field library that contains this kind of formatting information.
The field you'd be interested in is fmt. There doesn't seem to be any formal documentation on the format they use, but a proposal to include this information as part of the Unicode CLDR has matching fields (scroll down to "Detailed Breakdown of elements"); there are also some clues in Google's libaddressinput source code.

Is it possible to create a mask to handle non-north american phone numbers?

For north american phone numbers, (999) 999-9999 works pretty well for an input mask.
However, I can't find a good example that will handle non-north american numbers. I know that the number of digits can vary, so other than restricting it to digits only, is there a good example anywhere?
There is no generic mask, really: There are too many combinations.
The only thing that is fixed is the international country code, usually prefixed by +.
According to the Wikipedia Article on telephone numbering plans, most countries conform with the E.164 numbering plan.
If I read E.164 correctly, you can safely make the following assumptions:
Country code: 1-3 digits
Network / Area code and Number: Up to 19 digits
I would ask for the country code, and have the "area code + number" field as a 19-digit input.
You can deduce the country code with a simple RegEx such as:
^(?:(?:0(?:0|11)\s?)|+)([17]|2([07]|[1-689]\d)|3([0-469]|[578]\d)|4([013-9]|2\d)|5([1-8]|[09]\d)|6([0-6]|[789]\d)|8([12469]|[03578]\d)|9([0-58]|[679]\d))
Followed by
(([\s\(\).-]{0,2}\d){4,13})$
to extract the national number.
For validating the national number length and validity, you'd need libphonenumber or similar.
The long RegEx above allows +, 00 or 011 before the country code and a selection of punctuation in the number which will also have to be stripped.
You don't mention your application but this is certainly possible using regular expressions. You might want to take a look here.
Not easily. Take a look at this page for an example why: if you only look at the German phone numbers, you'll note that there are different formats depending on where you're calling the number from. Which one do you pick? And that's just for German phone numbers; they differ from continent to continent, and from country to country.
Going with "numbers-only" is probably your safest bet.
I would allow for spaces, dashes, slashes and all that, but actually only care for numbers and the optional leading + sign. Everything else, such as assuming certain blocks of a certain length is just asking for trouble.
May be it is bad to answer an old question. But libphonenumber seems like a good solution to your question.

Resources