Skip to content

Instantly share code, notes, and snippets.

@Andrew67
Created April 5, 2020 22:59
Show Gist options
  • Save Andrew67/b7b5c127ae901d0ba3e7d79e46263696 to your computer and use it in GitHub Desktop.
Save Andrew67/b7b5c127ae901d0ba3e7d79e46263696 to your computer and use it in GitHub Desktop.
<?php
// LICENSE: Public Domain
/**
* Converts a letter to its corresponding regional indicator Unicode character
* @param $chr string The letter to convert (A to Z, case insensitive). If outside this range, it silently wraps around in an undefined manner
* @return string The regional indicator Unicode character
*/
function letter_to_regional_indicator($chr) {
// 0x41 = capital letter A, while the modulo 26 clamps input to A-Z by wrapping around
$distance_from_A = (ord(strtoupper($chr)) - 0x41) % 26;
// 0x1F1E6 = regional indicator A
return mb_chr(0x1F1E6 + $distance_from_A, 'UTF-8');
}
/**
* Converts an ISO 3166-1 alpha-2 country code into its corresponding flag emoji
* @param $code string The country code. If input is incorrect, it silently produces an incorrect flag
* @return string The flag emoji characters. Unsupported combinations are rendered as the individual letters
*/
function country_code_to_flag_emoji($code) {
return letter_to_regional_indicator($code[0]) . letter_to_regional_indicator($code[1]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment