New in Symfony 6.3: Emoji Improvements
May 9, 2023 • Published by Javier Eguiluz
Symfony 6.3 is backed by:
Warning: This post is about an unsupported Symfony version. Some of this information may be out of date. Read the most recent Symfony Docs.
Emojis continue to be an important part of web applications, even in the enterprise environment. For that reason, in Symfony 6.3 we have continued to improve some emoji features.
Strip Emojis from Strings
Sometimes, for technical or style reasons, your application doesn't want to accept emojis in the contents provided by users. However, striping emojis from string contents is unnecessarily difficult when using regular expressions.
That's why in Symfony 6.3 we've added a functionality to remove all emojis from
a given string content. This feature is implemented via a new artificial locale
called strip
introduced in the emoji transliterator class:
1 2 3 4 5
use Symfony\Component\Intl\Transliterator\EmojiTransliterator;
$transliterator = EmojiTransliterator::create('emoji-strip');
$result = $transliterator->transliterate('A ๐บ, ๐โโฌ, and a ๐ฆ');
// $result = 'A , , and a '
You can also strip emojis when generating slugs:
1 2 3 4 5 6 7
use Symfony\Component\String\Slugger\AsciiSlugger;
$slugger = new AsciiSlugger();
$slugger = $slugger->withEmoji('strip');
$slug = $slugger->slug('a ๐บ, ๐โโฌ, and a ๐ฆ');
// $slug = 'a-and-a';
Compress Emoji Data
The previous emoji removal feature is not based on regular expressions. Instead, we built some replace rules using the almost 5,000 emojis defined in the Unicode project. These rules are updated automatically, so they will always contain all the official emojis.
The main drawback of this emoji support is that the data needed to transliterate emojis into all languages takes a lot of disk space. The Intl component weights around 44 MB, which can cause problems when using serverless services with tight size constraints.
We discussed about the option to create a new component to store just the emoji data, but decided to not do that to avoid the hassle of managing this. Instead, in Symfony 6.3 we've added a tool so you can compress all emoji data yourself:
1 2
# adjust the path to the 'compress' binary according to your application
$ php ./vendor/symfony/intl/Resources/bin/compress
This command replaces the original *.php
internal Symfony files that hold
the emoji data by some equivalent *.php.gz
files compressed with the zlib
PHP extension. That's all you need to do. Symfony will uncompress the .gz
files on the fly when it finds one.
Help the Symfony project!
As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.
Comments are closed.
To ensure that comments stay relevant, they are closed for old posts.
https://medium.com/@bobbyrsec/the-dangers-of-googles-zip-tld-5e1e675e59a5
the transliterator should have an option to change those character for URL.