none
country name from phone number RRS feed

  • Question

  • hello, i have a lot of users in my postgres db, they already have phone numbers. I want from these numbers to know which country belong every user. I want to store them in a dashboard where I have a map and when I hover in every country, the number of users appear.
    Tuesday, March 12, 2019 6:56 AM

All replies

  • Hi kevin101099,

    Thank you for posting here.

    For your question, you could get the different country number according to the link below.

    https://en.wikipedia.org/wiki/List_of_mobile_telephone_prefixes_by_country

    And then store the phone numbers with different countries.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, March 12, 2019 7:50 AM
    Moderator
  • Thank wendy, but it is not my case. I already have in my db a table users a filed called phone_number.

    I want to know from these numbers how many user I have in every country.

    Tuesday, March 12, 2019 8:36 AM
  • Hi kevin101099,

    You could use compare the phone to get its country. And then store the phone number to same collection according to the country.

    Could you provide a code sample for to test?

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, March 12, 2019 8:40 AM
    Moderator
  •  public long GetAllSpanishUsers()
            {
                var SpanishUsers= 0;

                try
                {
                    SpanishUsers= _MyDbName_DBContext.Accounts.Where(x => x.PhoneNumber.StartsWith("0034"))
                        .Select(a => a.Id).Count();
                }
                catch (Exception e)
                {
                    Log.Error(e);
                    SpanishUsers= -1;
                }

                return SpanishUsers;
            }

    //////// another function for France

    public long GetAllFrenchUsers()
            {
                var FrenchUsers= 0;

                try
                {
                    FrenchUsers= _MyDbName_DBContext.Accounts.Where(x => x.PhoneNumber.StartsWith("0033"))
                        .Select(a => a.Id).Count();
                }
                catch (Exception e)
                {
                    Log.Error(e);
                    FrenchUsers= -1;
                }

                return FrenchUsers;
            }

    So my question is that I want a fucntion or a library that give me the name of country from a phone number. So after that, I could make my dashboard and store all my users in a map and that map give me the number of users for every country. 

    Thanks a lot Wendy:)

    Tuesday, March 12, 2019 9:48 AM
  • The Wikipedia page that Wendy pointed to does have all the country codes.  Note that you have to be careful with this, because the country codes are not all the same length.  The US is 1, France is 33, Croatia is 385.  You need to check for a match in one-digit codes, then two-digit, then three-digit.

    Does your database actually store 0033 for numbers in France?  Because those zeros are not actually part of the number.  It the "old days", you had to dial 00 in the United States in order to dial internationally, but their phone number starts with 33.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Tuesday, March 12, 2019 10:00 PM
  • Hi,

    here is an example how you could do it:

            public class PhoneCountryCodeService
            {
                Dictionary<string, Dictionary<string, string>> countryCodes = new Dictionary<string, Dictionary<string, string>>
                {
                    { // Zone 1
                        "001",
                        new Dictionary<string, string>
                        {
                            { "", "USA Canada" },
                            { "242", "Bahamas" },
                            { "246 ", "Barbados" },
                            // ...
                        }
                    },
                    { // Zone 2
                        "002",
                        new Dictionary<string, string>
                        {
                            { "0", "Ägypten" },
                            { "11", "Südsudan" },
                            { "12 ", "Marokko" },
                            // ...
                        }
                    },
                    // ...
                };
                string unknown = "Unknown";
    
                public PhoneCountryCodeService() {}
    
                public PhoneCountryCodeService(Dictionary<string, Dictionary<string, string>> countryCodes, string unknown)
                {
                    this.countryCodes = countryCodes;
                    this.unknown = unknown;
                }
    
                public string GetCountryZone(string number)
                {
                    number = number.Replace("+", "00");
                    if (number.Length < 3)
                        throw new Exception("Phone number too short");
                    return number.Substring(0, 3);
                }
    
                public string GetCountryName(string number)
                {
                    var zonePrefix = GetCountryZone(number);
                    number = number.Replace("+", "00").Substring(3);
                    var maxLength = number.Length > 3 ? 3 : number.Length;
                    string result = null;
                    if(countryCodes.TryGetValue(zonePrefix, out Dictionary<string, string> zones))
                    {   
                        for (int i = maxLength; i>=0;--i)
                        {
                            if (zones.TryGetValue(number.Substring(0, i), out string output))
                            {
                                result = output;
                                break;
                            }
                        }
                    }
                    return result ?? unknown;
                }
            }
            public static void Main(string[] args)
            {
                var sut = new PhoneCountryCodeService();
                var number1 = "+201234";
                var number2 = "+131234";
                var number3 = "+12421234";
                var number4 = "+00000";
                var number5 = "+2111234";
                var number6 = "+1";
                var number7 = "+";
    
                var country1 = sut.GetCountryName(number1);
                var country2 = sut.GetCountryName(number2);
                var country3 = sut.GetCountryName(number3);
                var country4 = sut.GetCountryName(number4);
                var country5 = sut.GetCountryName(number5);
                var country6 = sut.GetCountryName(number6);
                string country7;
                try
                {
                    country7 = sut.GetCountryName(number7);
                }
                catch (Exception e) {
                    Console.WriteLine(e);
                }
            }


    But you have to complete all the data. How Wendy Zang and Tim Roberts said, you can see the country codes on the wiki page.



    • Edited by DerChris88 Tuesday, March 12, 2019 10:44 PM
    Tuesday, March 12, 2019 10:38 PM