I want to convert a country's ISO code to its name using the following function:
function convertcodes($in, $type){
$out = "";
$long = array('Afghanistan' , 'Åland Islands' , 'Albania' , 'Algeria' , 'American Samoa' , 'Andorra');
$short = array('af','ax','al','dz','as','ad');
$in = trim($in);
switch($type){
case 'long':
$out = str_replace($short, $long, $in);
break;
case 'short':
$out = str_replace($long, $short, $in);
break;
}
return $out;
}
The problem is that it returns ALL countries instead of the one that I'm looking for because its matching strings. How can I make it match the exact string? Using preg_replace won't work with an array.
(Obviously the actual arrays are much longer, I stripped a part here in order to not make my posted code too long.)