New in Symfony 6.3: Emoji Improvements
Symfony 6.3 is backed by:
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
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';
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:
# 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
PHP extension. That's all you need to do. Symfony will uncompress the
files on the fly when it finds one.
Comments are closed.
To ensure that comments stay relevant, they are closed for old posts.