MSISDN Country Code Definition

How can I determine the country code from the following MSISDN ?

Cyprus (+ 357XXXXXXXX) - 11 digits

Finland (+ 358XXXXXXXXXX) - 13 digits

Serbia (+ 381XXXXXXX) - 10 digits

Different countries have different numbers of digits in their country code as well as their MSISDN, so it is not possible to match a country code from just the whole number.

So how can I get the country code from a specific MSISDN?

+3


source to share


1 answer


If the data is in a fixed format:, {country_code} {space} {phone_digits}

you can easily use string operations (regex, split, etc.) to get the country code. To determine which country the code is represented in, you must match it against a list of known country codes - either store them in your database or use a web service for verification.

A possible approach is to retrieve the country codes from the database and manually compare when you find a match - if the format is not consistent. This can be error prone as country codes can also vary in length and you could match the wrong one. A somewhat acceptable approach, if there is no other choice, is to match the longest codes first and then the shorter codes, but there is no guarantee for invalid matches. To avoid the problem entirely, consider the following suggestion:

If you have control over how phone numbers are stored (in your database or whatever storage you use), then I would suggest that you store the country code in a separate field. For a SQL database, this would mean you have a column for the country code and a different phone number. Then you can easily extract the country code with absolute certainty and process it any way you want.




As you can see from the comments, this format is what you expect from your api clients. If so, you should try to apply some format constraints - force the client to pass the country code as a separate parameter, or use an appropriate separator character. If so, you are done analyzing the code and determining the country. The client must match the desired format.

+1


source







All Articles