PHP code example of tourze / gb-t-2659

1. Go to this page and download the library: Download tourze/gb-t-2659 library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tourze / gb-t-2659 example snippets




use Tourze\GBT2659\Alpha2Code;

// Basic usage
$china = Alpha2Code::CN;
echo $china->value;        // Output: CN
echo $china->getLabel();   // Output: 中国
echo $china->toLowerCase(); // Output: cn

// Create from string
$usa = Alpha2Code::from('US');
echo $usa->getLabel();     // Output: 美国

// Safe creation with tryFrom
$code = Alpha2Code::tryFrom('UK');
if ($code === null) {
    echo "Invalid code";
}

// Get all countries
$allCountries = Alpha2Code::cases();
echo count($allCountries); // Output: 249

// Generate options for dropdown
$options = Alpha2Code::genOptions();
// Returns: [['value' => 'CN', 'label' => '中国'], ...]

// Convert to array
$array = Alpha2Code::CN->toArray();
// Returns: ['value' => 'CN', 'label' => '中国']

// Convert to select item
$item = Alpha2Code::CN->toSelectItem();
// Returns: ['value' => 'CN', 'label' => '中国', 'text' => '中国', 'name' => '中国']

// Filter Asian countries
$asianCountries = [
    Alpha2Code::CN, Alpha2Code::JP, Alpha2Code::KR,
    Alpha2Code::IN, Alpha2Code::SG, Alpha2Code::TH
];

$userCountry = Alpha2Code::from('JP');
if (in_array($userCountry, $asianCountries)) {
    echo "User is from Asia";
}