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

Thomas Calvet
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:

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

Nicolas Grekas
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 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.

Published in #Living on the edge