Country Converter
A PHP package to convert a country ISO to country name and vice-versa., (*1)
Installation
To install this package, make sure you have composer.
Then, require it:, (*2)
composer require chibifr/country-converter
Usage
You can now use it really easily! After you required the package with composer, start using it:, (*3)
Without Exception management, (*4)
<?php
// Require the composer's vendor autoload file
require './vendor/autoload.php';
use ChibiFR\CountryConverter\Converter;
// Create a new Converter object
$converter = new Converter();
// Get a country name from its ISO code
echo $converter->getCountryName('jp'); // will echo "Japan"
// Get a country ISO code from its name
echo $converter->getCountryCode('France'); // will echo "FR"
With Exception management, (*5)
<?php
// Require the composer's vendor autoload file
require './vendor/autoload.php';
use ChibiFR\CountryConverter\Converter;
// Create a new Converter object
$converter = new Converter();
// Get a country name from its ISO code
try {
echo $converter->getCountryName('jp'); // will echo "Japan"
} catch (InvalidCountryNameException $e) {
die($e->getMessage());
}
// Get a country ISO code from its name
try {
echo $converter->getCountryCode('France'); // will echo "FR"
} catch (InvalidCountryCodeException $e) {
die($e->getMessage());
}