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:
Top comments (12)
So exciting. Thank you @block blast
A JS one-liner if you want one:
Nice one! Thanks π€©
Awesome
Thanks! I didn't realize there was an algorithm behind this.
No these luck: Jhonkensy Noel affiliated for a video game-tying 2-operate homer, a blast that Weaver couldn't deliver himself towards check out inside of genuine period. Joe DiMaggio Jersey
Inevitably turning into toward capture him and function with him upon the other aspect was absolutely pleasurable. philliessportsstore.com
With 2 outs, Kepler knocked inside of Larnach with a one and then scored upon a double by means of Willi Castro Kirby Puckett Jersey, who took a superior fastball the other route and banged it off the foundation of the wall within just entrance of the bullpen within just Twins did problems with 2 outs at the time yet again within just the backside of the fourth.
The merely 2 gamers that could possibly be regarded a light question below are Capel and Gillispie. Allan Winans Jersey
Set that with Geno's long term bad performance, and the Reds now have some $73 million linked up in a pair of 3Bs who do not include a lot anywhere else defensively and have not generated up to par offensively Pete Rose Jersey.
thank your very very much, @cheese chompers 3d .