Turn a country code into an emoji flag (US β‘οΈ πΊπΈ)
Flag emojis are a fun and visual way to represent countries and regions. These emojis are part of the Unicode standard and are created using a pair of regional indicator symbols.
In this article, we will explore how to convert 2-letter ISO 3166-1 country codes to flag emojis in Go, PHP and TypeScript.
Understanding Flag Emojis
Flag emojis are composed of two Unicode characters, each representing a regional indicator symbol. For example, the flag of the United States (πΊπΈ) is created by placing the Unicode points for the regional indicators "πΊ" and "πΈ" next to each other.
These Unicode points start at U+1F1E6 (regional indicator symbol letter "A") and continue to U+1F1FF (regional indicator symbol letter "Z").
The formula π€
The formula to convert a letter to its corresponding regional indicator symbol is:
Unicode point = <ASCII code of letter> β 65 + 127462
65
is a decimal value of letter "A"127462
is a decimal value ofU+1F1E6
Since 127462 - 65 = 127397 = 0x1F1A5
, we will use 0x1F1A5
in further calculations.
Conversion in Go π¨π»βπ»
Here is an example in Go (Golang) to convert an ISO 3166-1 country code to a flag emoji. This code snippet normalizes the input to uppercase and then converts each character to its corresponding regional indicator symbol. For simplicity, it doesn't perform any validation so you should make sure it is a valid 2-letter code first.
package main
import (
"fmt"
"strings"
)
func country2flag(countryCode string) string {
var flagEmoji strings.Builder
countryCode = strings.ToUpper(countryCode)
for _, char := range countryCode {
flagEmoji.WriteRune(rune(char) + 0x1F1A5)
}
return flagEmoji.String()
}
func main() {
fmt.Println(country2flag("pl")) // π΅π±
fmt.Println(country2flag("JP")) // π―π΅
fmt.Println(country2flag("us")) // πΊπΈ
fmt.Println(country2flag("EU")) // πͺπΊ
}
Explanation π¨π»βπ«
Normalization: The input country code is converted to uppercase using
strings.ToUpper()
.String Builder: A
strings.Builder
is used to efficiently build the resulting flag emoji string.Character Processing: Each character of the uppercase country code is processed:
rune(char) + 0x1F1A5
: Converts the character to its corresponding regional indicator symbol by adding0x1F1A5
(127397).Appending Characters: The resulting regional indicator symbols are appended to the
strings.Builder
.Returning the Result: The
String()
method ofstrings.Builder
is called to get the final flag emoji string.
Conversion in PHP π
Using preg_replace_callback
allows to replace each character in a string using a custom callback:
function country2flag(string $countryCode): string
{
return preg_replace_callback(
'/./',
static fn (array $m) => chr(ord($m[0]) + 0x1F1A5),
strtoupper($countryCode)
);
}
echo country2flag('pl'); // π΅π±
echo country2flag('JP'); // π―π΅
echo country2flag('us'); // πΊπΈ
echo country2flag('EU'); // πͺπΊ
Conversion in TypeScript π
This example uses a combination of split
and join
to process each character individually.
function country2flag(countryCode: string): string {
return countryCode
.toUpperCase()
.split('')
.map(char => String.fromCodePoint(char.charCodeAt(0) + 0x1F1A5))
.join('');
}
console.log(country2flag('pl')); // π΅π±
console.log(country2flag('JP')); // π―π΅
console.log(country2flag('us')); // πΊπΈ
console.log(country2flag('EU')); // πͺπΊ
Conclusion
π€ Converting ISO 3166-1 country codes to flag emojis can be fun and straightforward in Go, PHP and TypeScript! By understanding how regional indicator symbols work and using simple character manipulation, you can easily generate these emojis programmatically in any other language β¨.
π οΈ Whether you're building a web application or a server-side script, this technique can add a fun and informative visual element to your project.
βπ» Please note that the appearance and availability of the flag emojis might vary between systems and locale.
Feel free to post snippets in your favourite programming language in the comments!
This article is based on the snippet I posted on GitHub Gist 3 years ago:
Subscribe to my newsletter
Read articles from Ion Bazan directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Ion Bazan
Ion Bazan
I'm a Full Stack Software Developer born in Moldova π²π©, raised in Poland π΅π± and based in Singapore πΈπ¬, who is passionate about good practices π, clean code π§Ή, containerized environments π and good pizza π.