spatie/emoji
Work with emoji in PHP without relying on your IDE/font. Use the Spatie\Emoji\Emoji class to access emoji as constants or friendly camelCase methods like Emoji::grinningFace(), or fetch all emojis via Emoji::all().
Installation:
composer require spatie/emoji
No additional configuration is required—just autoload.
First Use Case: Convert text to emoji in a Blade view:
use Spatie\Emoji\Emoji;
// In a controller or service
$converted = Emoji::emojify('Hello :heart: world!');
echo $converted; // Outputs: Hello ♥ world!
Where to Look First:
Spatie\Emoji\Emoji class methods: emojify(), emojifyShortcodes(), emojifyUnicode().tests/ directory for edge cases.Text Conversion:
Emoji::emojify(':smile:') // → 😊
Emoji::emojifyShortcodes('I :thumbsup: this!') // → I ✅ this!
Emoji::emojifyUnicode('Already has 😊') // → 😊 (unchanged)
Blade Directives (for reusable templates):
// In AppServiceProvider::boot()
Blade::directive('emoji', function ($expression) {
return "<?php echo \\Spatie\\Emoji\\Emoji::emojify({$expression}); ?>";
});
// Usage in Blade:
@emoji('$message')
Dynamic Emoji Handling:
$cleanedText = Emoji::emojify($request->input('comment'));
return response()->json(['content' => Emoji::emojify($data['content'])]);
Custom Shortcodes: Extend the package by adding custom mappings (see "Gotchas" for implementation).
♥ as U+2665). Avoid shortcodes in DB if possible for consistency.Emoji::emojify() with locale-aware text (e.g., 🇺🇸 for flags).$cacheKey = 'emoji_'.$text;
$converted = Cache::remember($cacheKey, now()->addHours(1), fn() => Emoji::emojify($text));
Double Conversion: Converting already-converted text (e.g., Unicode → shortcode → Unicode) may corrupt emoji. Always check input type:
if (str_contains($text, ':')) {
return Emoji::emojifyShortcodes($text);
}
return Emoji::emojifyUnicode($text);
Shortcode Conflicts:
Shortcodes like :thumbsup: may clash with user input (e.g., :thumbs up:). Use emojifyShortcodes() sparingly or pre-process text.
Non-Standard Emoji: The package uses a subset of Unicode emoji. For custom emoji (e.g., Slack-style), extend the package (see below).
Blade Caching:
If using @emoji directives, clear Blade cache after adding custom shortcodes:
php artisan view:clear
Verify Emoji Support: Check if your terminal/editor supports Unicode output. Test with:
echo Emoji::emojify(':grinning:');
If it shows ?:grinning:?, your environment lacks Unicode support.
Log Unmatched Shortcodes:
Override the emojifyShortcodes method to log unknown shortcodes:
Emoji::emojifyShortcodes($text, true); // Second param enables logging
Custom Shortcodes:
Extend the Spatie\Emoji\Shortcode class or override the getShortcodeReplacements() method:
// In a service provider
Emoji::extend(function ($emoji) {
$emoji->addShortcode('custom', '🎨');
});
Custom Emoji Sets:
Replace the default emoji list by binding a custom Emoji instance:
$app->bind(Spatie\Emoji\Emoji::class, function ($app) {
return new CustomEmoji();
});
Whitelist/Blacklist: Filter emoji by Unicode block (e.g., block flags only):
$flags = Emoji::emojify('🇺🇸🇬🇧', ['flags']);
emojifyShortcodes() to convert Slack-style :emoji: to Unicode before displaying in Laravel apps.parsedown or commonmark to convert Markdown with emoji:
use League\CommonMark\CommonMarkConverter;
$converter = new CommonMarkConverter();
$html = $converter->convert(Emoji::emojify($markdownText));
EmojiTest class as a reference for unit tests:
$this->assertEquals('😊', Emoji::emojify(':smile:'));
How can I help you explore Laravel packages today?