views:

117

answers:

5

There are several methods on country codes.

I have a list of codes with 3-characters, like on this page:

http://www.fina.org/H2O/index.php?option=com_content&view=category&id=93:asia&Itemid=638&layout=default

Is there a simple way to convert them to 2-characters? Like "PT" from "POR" for Portugal.

Standard for 2-characters - http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2

Thanks.

+2  A: 

There is a similar question in gis.stackexchange.
http://gis.stackexchange.com/questions/603/is-a-country-state-city-database-available
I think you will get more information by posting this questions in gis. http://gis.stackexchange.com

Sandy
From what I understood Ignatz needs to convert 'POR' to 'PT', not IP to location
Im0rtality
@Im0rtality - yes I know, what if he get PT - PORTUGAL mapping database or list, rather than converting it? thats why I recommended posting this question in GIS.
Sandy
A: 

$mapping['POR'] = 'PT';
$shortcode = $mapping[$longcode];

Im0rtality
+3  A: 

Without doing an actual lookup, there is no simple way: AFG (Afghanistan) becomes AF, while AND (Andorra) becomes AD, and BLR (Belarus) becomes BY... so you can't do any simple character manipulation to convert.

My suggestion would be to use a countrycode table, or add an extra column to any existing table, so that you hold both codes.

Mark Baker
@Mark Baker - correct, thats why I recommend posting this question in gis.stackexchange.com
Sandy
Adding an extra column is definatly the best approach
Tom Gullen
+1  A: 

There is going to be no easy way because there is no particular scheme in the names of the country. For example PT from POR for Portugal and this can be different for other countries as well. You might want to create an array to hold two letters for each country.

Example:

$countries = array('PT' => 'Portugal', 'UK' => 'United Kingdom');
Sarfraz
+1  A: 

While this may be a lengthy and painful method, it may very well be worth your while writing a function that you can keep forever more, maybe this can point you in the right direction:

<?php
function myCodes($in, $type){
$out = "";
$long = array('portugal', 'united kingdom');
$short = array('pt', 'uk');
$in = strtolower(trim($in));
switch($type){
case 'long':$out = str_replace($short, $long, $in);break;
case 'short':$out = str_replace($long, $short, $in);break;
}
echo $out;
}

echo myCodes('United Kingdom', 'short'); //this will echo 'uk'
echo myCodes('UK', 'long'); //this will echo 'united kingdom'

?>

This will of course have a few drawbacks such as making sure that the arrays for long and short match up position wise, and you'll need to maintain the function as well.

webfac