Skip to content

Instantly share code, notes, and snippets.

@Mattia98
Created October 20, 2021 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mattia98/b661ccba2abb6e3221a8210a371ea454 to your computer and use it in GitHub Desktop.
Save Mattia98/b661ccba2abb6e3221a8210a371ea454 to your computer and use it in GitHub Desktop.
PHP function to convert country code to flag emoji
<?php
// Assumes country code to already be uppercase
function countryCodeToEmoji($cc) {
return
mb_chr(127397 + ord($cc[0])) .
mb_chr(127397 + ord($cc[1]));
}
// Makes sure the country code is uppercase
function countryCodeToEmojiCaseSafe($cc) {
$cc = strtoupper($cc);
return
mb_chr(127397 + ord($cc[0])) .
mb_chr(127397 + ord($cc[1]));
}
// This function generally makes some assumptions, like the string should be a valid country code.
// This should be fine for most usecases
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment