Jump to content

[SOLVED] Identifying countries from IDD number


peroxide

Recommended Posts

I've been trying to find the best way to do this, but can't think of anything

 

Suppose I had a list of IDD numbers, e.g. +6141234567, +658123948, +1771724762 etc in a variable $ccode

I have a mysql table with columns country_code, country_name with contents like

country_codecountry_name

61Australia

65Singapore

etc etc..

What's the best way to match $ccode with the table to find the appropriate country name?

 

Thanks in advance.

Link to comment
Share on other sites

A step in good direction for sure, but imagine how many ifs you would have for all countries. Close to 200 in present day I believe.

Better create an array (which can be loaded from database) and then compare a string against its elements.

 

$codes = array("61" => "Australia", "65" => "Singapore", /*....*/);
$ccode  = "6123526344";

foreach($codes as $code => $country) {
if(strstr($ccode,$code)) {
  echo $country;
}
}

 

I am not convinced that strstr would be the best function here... but it might be. WOuld have to review IDD codes for that. ;)

Link to comment
Share on other sites

A step in good direction for sure, but imagine how many ifs you would have for all countries. Close to 200 in present day I believe.

Better create an array (which can be loaded from database) and then compare a string against its elements.

 

$codes = array("61" => "Australia", "65" => "Singapore", /*....*/);
$ccode  = "6123526344";

foreach($codes as $code => $country) {
if(strstr($ccode,$code)) {
  echo $country;
}
}

 

I am not convinced that strstr would be the best function here... but it might be. WOuld have to review IDD codes for that. ;)

 

That's a better solution, although I'd rather loop through the rows returned by the database to find a match rather than having to load it into an array first. Thanks, I know where to go from here now, although I would still like to hear from anyone who has an even better solution. Cheers!

Link to comment
Share on other sites

That's a better solution, although I'd rather loop through the rows returned by the database to find a match rather than having to load it into an array first.

 

That's also a good idea. The best one would be to get only one row from query. The one that matches the code given. ;)

Link to comment
Share on other sites

The best one would be to get only one row from query. The one that matches the code given. ;)

 

which could be done by using substr:

"SELECT * FROM countryCodes WHERE country_code = '".substr($ccode, 1, 2)."'";

this assumes $ccode is in format "+xxxxxxxxxx", with the heading "+", and that the countery_code is the first 2 digits, and that it is 2 only digits long. otherwise, the second argument of substr() should be 0 (starting point in the string when there is no +), and the third argument should be whatever length the country_code is.

Link to comment
Share on other sites

strstr() definitely isn't the correct route. Consider +44161423985, which is a UK number ('+44'), but which contains '61'. strstr() would return this as an Australian number.

 

You're looking for the IDD code at the beginning of the number, and they're also of varying length, so:

$codes = array("61" => "Australia", "65" => "Singapore", /*....*/);
$ccode  = "6123526344";

foreach($codes as $code => $country) {
   if (substr($ccode,0,strlen($code)) == $code) {
      echo $country;
   }
}

But as bobbinsbro says, you're better off doing this via a database query.

Link to comment
Share on other sites

Preprocessing the query using substr wouldn't work, since there are 1, 2 and 3 character country codes. As for it accidentally matching a string in the middle of another number, that wouldn't happen because the returned value would not be equal to the initial value.

I.E.

 

$string1 = "448473612";
$string2 = "61";
echo strstr($string1,$string2); //returns '612'
$string1 = "611624873";
echo strstr($string1,$string2); //returns '611624873'

 

That's why I put what I put in the IF conditions.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.