任何类别将国家/地区名称变为2个字母的代码?

问题描述:

I would like to get some class to turn a full country name like United States into a 2 letters ISO country code US

P.S. i don't prefer to call something like google API for this.

Thanks!

我想让一些课程将完整的国家/地区名称(如 United States code>)转换为 一个2个字母的ISO国家代码 US code> p>

PS 我不喜欢为此调用像谷歌API这样的东西。 p>

谢谢! p> div>

How about the array here?

You could then call it as follows:

$code = array_search('United States', $countrycodes); // returns 'US'
$country = $countrycodes['US']; // returns 'United States'

I made a small and growing collection of classes on github that contain constants for this purpose. you don't have to search for it and you can get hints via your IDE as your typing. Helpfull if you can't remember all the country names and spellings.

It also can do country codes to country names :P

Save this code as cc.php

include_once 'cc.php';
$iso_code = array_search(strtolower($country_that_you_want_to_convert), array_map('strtolower', $countrycodes)); ## easy version

or we can make Function like this:

Version 1:

include_once 'cc.php';
function get_iso_code($country_that_you_want_to_convert, $countrycodes){
     $iso_code = array_search(strtolower($country_that_you_want_to_convert), array_map('strtolower', $countrycodes)); ## easy version
 return $iso_code;
}

Version 2:

function get_iso_code($country_that_you_want_to_convert){
include_once 'cc.php';
     $iso_code = array_search(strtolower($country_that_you_want_to_convert), array_map('strtolower', $countrycodes)); ## easy version
 return $iso_code;
}

Sorry if I made mistakes. if got only downvote I will happy to remove my post. hope its help someone.