New in Symfony 6.3: Emoji Improvements
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
Contributed by
Thomas Calvet
in #48396.
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:
use SymfonyComponentIntlTransliteratorEmojiTransliterator;
$transliterator = EmojiTransliterator::create(‚emoji-strip‘);
$result = $transliterator->transliterate(‚A ?, ?⬛, and a ?‘);
// result: ‚A , , and a ‚
You can also strip emojis when generating slugs:
use SymfonyComponentStringSluggerAsciiSlugger;
$slugger = new AsciiSlugger();
$slugger = $slugger->withEmoji(’strip‘);
$slug = $slugger->slug(‚a ?, ?⬛, and a ?‘);
// $slug = ‚a-and-a‘;
Compress Emoji Data
Contributed by
Nicolas Grekas
in #50055.
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 is a waste of traffic and disk space and 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:
# 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.
Symfony Blog