peroxide Posted November 9, 2008 Share Posted November 9, 2008 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. Quote Link to comment Share on other sites More sharing options...
peroxide Posted November 9, 2008 Author Share Posted November 9, 2008 Update: I found a way to do this using strstr function, as in $ccode = "6123526344"; if(strstr($ccode,country_code)==$ccode) { echo "Australia"; } Would this be the ideal solution? Quote Link to comment Share on other sites More sharing options...
Mchl Posted November 9, 2008 Share Posted November 9, 2008 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. Quote Link to comment Share on other sites More sharing options...
peroxide Posted November 9, 2008 Author Share Posted November 9, 2008 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! Quote Link to comment Share on other sites More sharing options...
Mchl Posted November 9, 2008 Share Posted November 9, 2008 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. Quote Link to comment Share on other sites More sharing options...
bobbinsbro Posted November 9, 2008 Share Posted November 9, 2008 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. Quote Link to comment Share on other sites More sharing options...
Mchl Posted November 9, 2008 Share Posted November 9, 2008 Well.. if IDDs where as easy as that. I think it's case for LIKE() function Quote Link to comment Share on other sites More sharing options...
bobbinsbro Posted November 9, 2008 Share Posted November 9, 2008 oh, damn. lol Quote Link to comment Share on other sites More sharing options...
Mark Baker Posted November 9, 2008 Share Posted November 9, 2008 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. Quote Link to comment Share on other sites More sharing options...
Mchl Posted November 9, 2008 Share Posted November 9, 2008 strstr returns first occurence of string, so actually it would work good as long as there's + at the beginning of both strings. Quote Link to comment Share on other sites More sharing options...
peroxide Posted November 9, 2008 Author Share Posted November 9, 2008 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. Quote Link to comment Share on other sites More sharing options...
Mark Baker Posted November 9, 2008 Share Posted November 9, 2008 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. Quote Link to comment Share on other sites More sharing options...
Mark Baker Posted November 9, 2008 Share Posted November 9, 2008 Preprocessing the query using substr wouldn't work, since there are 1, 2 and 3 character country codes.That's why I put in the strlen() test Quote Link to comment Share on other sites More sharing options...
peroxide Posted November 10, 2008 Author Share Posted November 10, 2008 I'm going with strstr for now.. any other suggestions? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.