0

how explode country code from number php, i need 1-3 characters from number for country validation. I have array with ( code => country), i have script example, but it works only if i explode 1 character, but country code can be 2 or 3 characters too. i know that i can explode by $number = explode(" ", 1 777777); , but i need full number without additional symbols or spaces.

$number   = "1777777";
$we       = substr($number, 0, 2);
$countrys = array(
                  '1' => 'us',
                  '2' => 'uk',
                  '3' => 'de',
                  '44' => 'fi',    // dont work if 2 characters..
                  '123' => 'no'    // dont work if 3 characters..
                );
$array = "$we";
echo $country = $countrys[$array[0]];
echo "<br>country";

how i can modificate this code? thank you! or any other suggestions?

lighter
  • 2,808
  • 3
  • 40
  • 59
toritumae
  • 3
  • 4
  • what do you mean? phone number can be different length how i can check country code from the end? – toritumae Mar 13 '14 at 03:45
  • explode right to left http://stackoverflow.com/questions/717328/how-to-explode-string-right-to-left – Thusitha Sumanadasa Mar 13 '14 at 04:11
  • 1
    You only have these 5 countries ? IF not there will be problem. If you think dynamically, What if the phone number is **`[12][3456789]`** and there are country codes like **`[12]`** and **`[123]`** ? It returns the country of **`[123]`** not **`[12]`** OR **`[12]`** not **`[123]`** when you loop through the codes. – Ijas Ameenudeen Mar 13 '14 at 04:21
  • 1
    IJas: This case is impossible because of the design of the country calling codes. If there is a country code "123" there will not be a "12" or a "1". Otherwise it would be impossible to call someone in another country because the numbers would not be distinct. Therefore all of the sorting efforts below are more a theoretical effort, or they are important if the list is not complete and he uses "classes" of numbers. In this case "123" would be more specific than "1". – Pascal Ockert Mar 13 '14 at 04:32
  • Ok, I didn't know that **`design of coutry codes`**. +1 for that. What if there numbers like **`[1][2420000]`** (USA) & **`[1242][00000]`** (BAHAMAS) ? 2nd one gets the priority ? – Ijas Ameenudeen Mar 13 '14 at 04:46
  • This [list of country code](http://en.wikipedia.org/wiki/List_of_country_calling_codes) have some overlapping prefixes, although some are already defunct or part of the parent country (+44 for England vs +441534 for Jersey). An exception seems to be +39 for Italy and +39-06-698 for Vatican City. – Sutandiono Mar 13 '14 at 14:40
  • This depends on the country's numbering plans. Sutandiono: +39 is Italy, +39 06 is Rome and +39 06 698 is Vatican City. This structure simply represents the geographic hierarchy. IJas: I am not familiar with US phone numbers but I guess that the format is +1 xxx where xxx is a reserved code for a region or city. In this case 242 is reserved for the Bahamas and there cannot be other numbers in the "+1" numbering plan that begin with 242. – Pascal Ockert Mar 13 '14 at 15:31

3 Answers3

2

I would assume if the number is like 123777777, the country code would be considered as 123 instead of 12 or 1, if both 1, 12 and 123 exist.

In this case, just start by checking the first 3 digits against the countrys array. If none is found, then go on by checking the first 2 digits, and so on.

    $number = "123777777";

    $countrys = array(

    '1' => 'us',
    '2' => 'uk',
    '3' => 'de',
    '44' => 'fi',
    '123' => 'no',
    );

    $i = 3;
    $country = "";
    while ($i > 0) {
        if (isset($countrys[substr($number, 0, $i)])) {
            $country = $countrys[substr($number, 0, $i)];
            break;
        } else {
            $i--;
        }
    }
    echo $country;
d.yuk
  • 809
  • 1
  • 12
  • 30
  • What if the phone number is **`[12][3456789]`** and there are country codes like **`[12]`** and **`[123]`** ? It returns the country of **`[123]`** not **`[12]`** right ? – Ijas Ameenudeen Mar 13 '14 at 04:11
  • That's why I made the assumption there. 'Cause we would not be able to tell if the number is [12][3456789] or [123][456789]. – d.yuk Mar 13 '14 at 04:36
  • So I would assume we always take [123] first, if both [123] and [12] are valid codes. – d.yuk Mar 13 '14 at 04:37
0

Use krsort to sort the array (longest numbers first). In this case "123" will be checked before "1". Then you can simply iterate over your array and check if the number begins with the country's number. If it matches, save the country code to a variable:

$number = '123777777';

$countrys = array(
  '1' => 'us',
  '2' => 'uk',
  '3' => 'de',
  '44' => 'fi',
  '123' => 'no'
);

// sort the array by key, descending
krsort($countrys, SORT_NUMERIC);

// iterate over all countries
foreach ($countrys as $we => $ccode) {
  // if number begins with current number
  if (strpos($number, '' . $we) === 0) {
    // store country code and break the loop
    $country = $ccode;
    break;
  }
}

echo 'country: ' . $country; // expected: "no"
Pascal Ockert
  • 984
  • 5
  • 10
0

In your code, you are always assigning the first country in $countrys to your $country. So you will always end up with us.

Instead of doing that, you should get the first 3, 2, or 1 digit(s) from your phone number, and check if the array key exists. If it does, you use that as the lookup index to your array.

Here's a pseudocode for you to get started:

perform a loop to get the first 3, 2, and 1 digit(s) from the phone number:  
  // Order is important as you want to match '123' before you match '1'  
  Check if the array key for the digit exists in the $countrys  
     If yes, get the country name and quit the loop  
     If not, go to the next less digit     

If the key is found:  
   Print the country name  
Else:  
   Print the country is not found  

Here is partial code for how you'd do it for the 3 digits. You just need to add a loop (such as for-loop) to simplify your work.

$number = "123777777";

// Add your loop here to check for 3, 2, 1 digit
$we = substr($number, 0, 3);

$countrys = array(
    '1' => 'us',
    '2' => 'uk',
    '3' => 'de',
    '44' => 'fi',
    '123' => 'no'
);

$array = "$we"; // You don't need this

if (array_key_exists ($we, $countrys))
{
    // Remember to break out of your loop when the country is found
    $country = $countrys[$we];
}
else
{
    $country = "Non in list";
}
// End your loop here

echo "${we} is ${country}\n";
Sutandiono
  • 1,748
  • 1
  • 12
  • 21
  • This will not work. What if the phone number is **`44777777`** ? **`$we`** will be **`447`** according to your code, not **`44`** as he expects. Check http://ideone.com/Yyuluc – Ijas Ameenudeen Mar 13 '14 at 04:33
  • I only posted partial code for 3 digit country code, hoping that the OP will continue the exercise on his/her own. :) I specified in the pseudocode for the OP to perform a loop which first checks for 3 digits, then 2 digits, then 1 digit. I put that in the code's comment as well :) – Sutandiono Mar 13 '14 at 04:41