Turn a country code into an emoji flag (US β‘οΈ πΊπΈ)

Search for a command to run...

No comments yet. Be the first to comment.
If youβre like many developers, you know the frustration of being stuck on an older PHP version. Upgrading to the latest and greatest can be a daunting task, especially when you're dealing with legacy code and tight deadlines π€¬. But fear not! Symfon...

Have you ever wished some queued jobs would be processed way ahead others? For example, if you want your VIP client's transanctions to be completed before everyone else's, this article might help you. Simple approach β¨ If you have only a few tiers of...

As a part of your digital identity, you may want to use a custom domain for your personal email address. In this article I'll show you how to easily set up email receiving and sending without breaking a bank. Why do I need a custom email domain in th...

When reviewing a Pull Request that touched composer.json and composer.lock files you might notice that the diffs are usually pretty huge and it's almost impossible to tell what exactly happened there - which packages got added, updated, downgraded or...

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.
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 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 of U+1F1E6
Since 127462 - 65 = 127397 = 0x1F1A5, we will use 0x1F1A5 in further calculations.
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")) // πͺπΊ
}
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 adding 0x1F1A5 (127397).
Appending Characters: The resulting regional indicator symbols are appended to the strings.Builder.
Returning the Result: The String() method of strings.Builder is called to get the final flag emoji string.
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'); // πͺπΊ
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')); // πͺπΊ
π€ 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: