3

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

Cyprus (+357XXXXXXXX) -- 11 digits

Finland (+358XXXXXXXXXX) -- 13 digits

Serbia (+381XXXXXXX) -- 10 digits

Different countries have different number of digits in their country code and also in their MSISDN, so, there is no way to match the country code alone from the entire number.

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

Ivaylo Slavov
  • 8,839
  • 12
  • 65
  • 108
user2432443
  • 147
  • 2
  • 11

1 Answers1

1

If the data is present in a fixed format: {country_code} {space} {phone_digits}, you could easily use string operations (regex, splitting and so on) to get the country code part. To determine which country the code represents, you should match it against a list of known country codes - either store these in your database or use a web service for the validation.

A possible approach is to get from a database the country codes, and compare manually where you find a match - in case the format is not consistent. This could be error prone, as country codes may also differ in length and you could match the wrong one. A somewhat acceptable approach, if there is no other choice, is to first match the longest, and then the shorter codes - yet there is no guarantee for invalid matches. To completely avoid the issue, consider the next suggestion:

If you have control over how the phone numbers are persisted (in your database or whatever storage you use), I'd advice you to store the country code in a separate field. For SQL database, this will mean you have a column for the country code and another for the phone number. You can then easily extract the country code with absolute certainty and process it in whatever way you like.


As seen from comments, the format in question is something you expect your api clients to provide. If that is the case, you should attempt to enforce some constraints to the format - make the client pass the country code as a separate parameter, or use a separator symbol that would be appropriate. If so, you will end up parsing the code and determining the country. It will be up to the client to conform to the desired format.

Ivaylo Slavov
  • 8,839
  • 12
  • 65
  • 108
  • Thanks for your answer, I have edited my question a bit, there is no separator between the country code and the destination number, this is exactly where I'm stuck, how can I know up to which length I have to consider as country code? Any suggestion will be appreciated. – user2432443 Jun 01 '15 at 08:45
  • This is indeed the worst case. Do you happen to know something else to constrain your guesses - like the country the phone is from? Do these phones come from your application or from somewhere else? It will be good to add this info to the question, rather in comment - just ping when edited – Ivaylo Slavov Jun 01 '15 at 09:17
  • It's like someone hitting my application http://myip/file?msisdn=XXXXXXX&bla=bla&bla=bla, I have to decide the country code of the MSISDN No, I don't know the country the phone is from, that's what I have to find out – user2432443 Jun 01 '15 at 09:32
  • Why don't you enforce a format for the URL parameters - like the number being with some kind of a separator (a slash, or a space - just make sure it is properly encoded). This will make it possible to distinguish the appropriate country code and leave it entirely up to the API consumer to supply you with proper data. – Ivaylo Slavov Jun 01 '15 at 09:35