PHP intl country code 2 chars to country name

后端 未结 8 1100
猫巷女王i
猫巷女王i 2021-02-18 17:57

I\'m triying to convert, using PHP, 2 characters country code (ISO 3166-1) to country names, or intl country codes, so I can translate the country name.

Using Loca

8条回答
  •  佛祖请我去吃肉
    2021-02-18 18:28

    Maybe a bit late...

    I wrote a small library in order to do this and some other ISO related things.

    https://github.com/php-components/isocodes

    Would be great to get some feedback or even pull requests in order to impove that library.

    use ISOCodes\ISO3166_1\Adapter\Json as ISO3166_1Adapter;
    
    $countryAdapter = new ISO3166_1Adapter();
    // Get the country with alpha-2 code 'NL'
    $country = $countryAdapter->get('nl');
    
    if (null === $country) {
        die('Country not found!');
    }
    
    // Get the french (fr) name.
    echo $country->getName('fr');
    

    The result would be Pays-Bas

    You can use alpha-2, alpha-3 or numeric code to do the country search.

提交回复
热议问题