Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • SensioLabs Professional services to help you with Symfony
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by
  1. Home
  2. Documentation
  3. Working with Emojis

Working with Emojis

Edit this page

7.1

The emoji component was introduced in Symfony 7.1.

Symfony provides several utilities to work with emoji characters and sequences from the Unicode CLDR dataset. They are available via the Emoji component, which you must first install in your application:

1
$ composer require symfony/emoji

Note

If you install this component outside of a Symfony application, you must require the vendor/autoload.php file in your code to enable the class autoloading mechanism provided by Composer. Read this article for more details.

The data needed to store the transliteration of all emojis (~5,000) into all languages take a considerable disk space.

If you need to save disk space (e.g. because you deploy to some service with tight size constraints), run this command (e.g. as an automated script after composer install) to compress the internal Symfony emoji data files using the PHP zlib extension:

1
2
# adjust the path to the 'compress' binary based on your application installation
$ php ./vendor/symfony/emoji/Resources/bin/compress

Emoji Transliteration

The EmojiTransliterator class offers a way to translate emojis into their textual representation in all languages based on the Unicode CLDR dataset:

1
2
3
4
5
6
7
8
9
10
11
use Symfony\Component\Emoji\EmojiTransliterator;

// Describe emojis in English
$transliterator = EmojiTransliterator::create('en');
$transliterator->transliterate('Menus with 🍕 or 🍝');
// => 'Menus with pizza or spaghetti'

// Describe emojis in Ukrainian
$transliterator = EmojiTransliterator::create('uk');
$transliterator->transliterate('Menus with 🍕 or 🍝');
// => 'Menus with піца or спагеті'

Tip

When using the slugger from the String component, you can combine it with the EmojiTransliterator to slugify emojis.

Transliterating Emoji Text Short Codes

Services like GitHub and Slack allows to include emojis in your messages using text short codes (e.g. you can add the :+1: code to render the 👍 emoji).

Symfony also provides a feature to transliterate emojis into short codes and vice versa. The short codes are slightly different on each service, so you must pass the name of the service as an argument when creating the transliterator.

GitHub Emoji Short Codes Transliteration

Convert emojis to GitHub short codes with the emoji-github locale:

1
2
3
$transliterator = EmojiTransliterator::create('emoji-github');
$transliterator->transliterate('Teenage 🐢 really love 🍕');
// => 'Teenage :turtle: really love :pizza:'

Convert GitHub short codes to emojis with the github-emoji locale:

1
2
3
$transliterator = EmojiTransliterator::create('github-emoji');
$transliterator->transliterate('Teenage :turtle: really love :pizza:');
// => 'Teenage 🐢 really love 🍕'

Gitlab Emoji Short Codes Transliteration

Convert emojis to Gitlab short codes with the emoji-gitlab locale:

1
2
3
$transliterator = EmojiTransliterator::create('emoji-gitlab');
$transliterator->transliterate('Breakfast with 🥝 or 🥛');
// => 'Breakfast with :kiwi: or :milk:'

Convert Gitlab short codes to emojis with the gitlab-emoji locale:

1
2
3
$transliterator = EmojiTransliterator::create('gitlab-emoji');
$transliterator->transliterate('Breakfast with :kiwi: or :milk:');
// => 'Breakfast with 🥝 or 🥛'

Slack Emoji Short Codes Transliteration

Convert emojis to Slack short codes with the emoji-slack locale:

1
2
3
$transliterator = EmojiTransliterator::create('emoji-slack');
$transliterator->transliterate('Menus with 🥗 or 🧆');
// => 'Menus with :green_salad: or :falafel:'

Convert Slack short codes to emojis with the slack-emoji locale:

1
2
3
$transliterator = EmojiTransliterator::create('slack-emoji');
$transliterator->transliterate('Menus with :green_salad: or :falafel:');
// => 'Menus with 🥗 or 🧆'

Universal Emoji Short Codes Transliteration

If you don't know which service was used to generate the short codes, you can use the text-emoji locale, which combines all codes from all services:

1
2
3
4
5
6
7
8
9
10
11
$transliterator = EmojiTransliterator::create('text-emoji');

// Github short codes
$transliterator->transliterate('Breakfast with :kiwi-fruit: or :milk-glass:');
// Gitlab short codes
$transliterator->transliterate('Breakfast with :kiwi: or :milk:');
// Slack short codes
$transliterator->transliterate('Breakfast with :kiwifruit: or :glass-of-milk:');

// all the above examples produce the same result:
// => 'Breakfast with 🥝 or 🥛'

You can convert emojis to short codes with the emoji-text locale:

1
2
3
$transliterator = EmojiTransliterator::create('emoji-text');
$transliterator->transliterate('Breakfast with 🥝 or 🥛');
// => 'Breakfast with :kiwifruit: or :milk-glass:

Inverse Emoji Transliteration

Given the textual representation of an emoji, you can reverse it back to get the actual emoji thanks to the emojify filter:

1
2
3
{{ 'I like :kiwi-fruit:'|emojify }} {# renders: I like 🥝 #}
{{ 'I like :kiwi:'|emojify }}       {# renders: I like 🥝 #}
{{ 'I like :kiwifruit:'|emojify }}  {# renders: I like 🥝 #}

By default, emojify uses the text catalog, which merges the emoji text codes of all services. If you prefer, you can select a specific catalog to use:

1
2
3
4
{{ 'I :green-heart: this'|emojify }}                  {# renders: I 💚 this #}
{{ ':green_salad: is nice'|emojify('slack') }}        {# renders: 🥗 is nice #}
{{ 'My :turtle: has no name yet'|emojify('github') }} {# renders: My 🐢 has no name yet #}
{{ ':kiwi: is a great fruit'|emojify('gitlab') }}     {# renders: 🥝 is a great fruit #}

Removing Emojis

The EmojiTransliterator can also be used to remove all emojis from a string, via the special strip locale:

1
2
3
4
5
use Symfony\Component\Emoji\EmojiTransliterator;

$transliterator = EmojiTransliterator::create('strip');
$transliterator->transliterate('🎉Hey!🥳 🎁Happy Birthday!🎁');
// => 'Hey! Happy Birthday!'
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version

    Symfony 7.1 is backed by

    Symfony Code Performance Profiling

    Symfony Code Performance Profiling

    Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).

    Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).

    Version:

    Table of Contents

    • Emoji Transliteration
    • Transliterating Emoji Text Short Codes
      • GitHub Emoji Short Codes Transliteration
      • Gitlab Emoji Short Codes Transliteration
      • Slack Emoji Short Codes Transliteration
      • Universal Emoji Short Codes Transliteration
    • Inverse Emoji Transliteration
    • Removing Emojis

    Symfony footer

    Avatar of Nicolas Bastien, a Symfony contributor

    Thanks Nicolas Bastien (@nicolas_bastien) for being a Symfony contributor

    2 commits • 12 lines changed

    View all contributors that help us make Symfony

    Become a Symfony contributor

    Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

    Learn how to contribute

    Symfony™ is a trademark of Symfony SAS. All rights reserved.

    • What is Symfony?

      • What is Symfony?
      • Symfony at a Glance
      • Symfony Components
      • Symfony Releases
      • Security Policy
      • Logo & Screenshots
      • Trademark & Licenses
      • symfony1 Legacy
    • Learn Symfony

      • Symfony Docs
      • Symfony Book
      • Reference
      • Bundles
      • Best Practices
      • Training
      • eLearning Platform
      • Certification
    • Screencasts

      • Learn Symfony
      • Learn PHP
      • Learn JavaScript
      • Learn Drupal
      • Learn RESTful APIs
    • Community

      • Symfony Community
      • SymfonyConnect
      • Events & Meetups
      • Projects using Symfony
      • Contributors
      • Symfony Jobs
      • Backers
      • Code of Conduct
      • Downloads Stats
      • Support
    • Blog

      • All Blog Posts
      • A Week of Symfony
      • Case Studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Living on the edge
      • Releases
      • Security Advisories
      • Symfony Insight
      • Twig
      • SensioLabs Blog
    • Services

      • SensioLabs services
      • Train developers
      • Manage your project quality
      • Improve your project performance
      • Host Symfony projects

      Powered by

    Follow Symfony