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.

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. ;)

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!

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. ;)

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.

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.

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.

strstr returns first occurence of string, so actually it would work good as long as there's + at the beginning of both strings

Agreed, as long as there is a '+' at the beginning of both strings... but I didn't see that in any of the examples above.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.