Created
October 20, 2021 15:32
-
-
Save Mattia98/b661ccba2abb6e3221a8210a371ea454 to your computer and use it in GitHub Desktop.
PHP function to convert country code to flag emoji
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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