symfony/emoji
Symfony Emoji component: access Unicode CLDR emoji characters and sequences in PHP. Includes a helper to compress bundled emoji data when zlib is enabled. Documentation and contributions are managed through the main Symfony repository.
composer require symfony/emoji
php vendor/symfony/emoji/Resources/bin/compress
use Symfony\Component\Emoji\Emoji;
// Check if a string contains emoji
Emoji::isEmoji('π'); // true
// Get emoji by name
Emoji::get('heart_eyes'); // "π"
Validate user input (e.g., reactions, usernames) to ensure only emoji are allowed:
use Symfony\Component\Emoji\Emoji;
public function storeReaction(Request $request)
{
$emoji = $request->input('emoji');
if (!Emoji::isEmoji($emoji)) {
return back()->withErrors(['emoji' => 'Invalid emoji']);
}
// Proceed with storage
}
Emoji: Core class for emoji operations (validation, lookup, normalization).EmojiData: Access emoji metadata (categories, keywords, Unicode blocks).EmojiMapper: Map emoji to/from text sequences (e.g., :smile: β "π").Use Case: Enforce emoji-only fields (e.g., reactions, usernames). Pattern:
use Symfony\Component\Emoji\Emoji;
// Single emoji check
if (Emoji::isEmoji($input)) {
// Valid
}
// Multi-emoji string (e.g., "ππ₯")
if (preg_match('/^[\p{Emoji}]+$/u', $input) && Emoji::isEmoji($input)) {
// Valid multi-emoji string
}
Laravel Integration:
// Form request validation
public function rules()
{
return [
'reaction' => ['required', function ($attribute, $value, $fail) {
if (!Emoji::isEmoji($value)) {
$fail('The :attribute must be a valid emoji.');
}
}],
];
}
Use Case: Standardize emoji sequences (e.g., family emoji, skin tones) before storage. Pattern:
use Symfony\Component\Emoji\Emoji;
// Normalize emoji sequences (e.g., "π¨βπ©βπ§βπ¦" β standardized form)
$normalized = Emoji::normalize('π¨βπ©βπ§βπ¦');
// Get emoji by name (e.g., "family" β "π¨βπ©βπ§βπ¦")
$emoji = Emoji::get('family');
// Get all emoji in a category (e.g., "Smileys & Emotion")
$smileys = EmojiData::get('Smileys & Emotion');
Laravel Service Provider:
// Register Emoji service for app-wide access
public function register()
{
$this->app->singleton('emoji', function () {
return new Emoji();
});
}
Use Case: Power a frontend emoji picker with categorized suggestions. Pattern:
use Symfony\Component\Emoji\EmojiData;
// Get emoji by keyword (e.g., "heart")
$hearts = EmojiData::getByKeyword('heart');
// Get emoji by Unicode block (e.g., "Smileys")
$smileys = EmojiData::getByUnicodeBlock('Smileys & Emotion');
// Get emoji with skin tones (e.g., "π¨βπ©βπ§βπ¦")
$familyEmoji = EmojiData::getWithSkinTones('family');
Blade Directive Example:
// Add to AppServiceProvider
Blade::directive('emoji', function ($expression) {
return "<?php echo Symfony\Component\Emoji\Emoji::get({$expression}); ?>";
});
// Usage in Blade
@emoji('heart_eyes') // Outputs: π
Use Case: Support emoji aliases (e.g., :heart: β "β€οΈ") for non-English users.
Pattern:
use Symfony\Component\Emoji\EmojiMapper;
// Map text to emoji (e.g., ":heart:" β "β€οΈ")
$mapper = new EmojiMapper();
$emoji = $mapper->map(':heart:'); // "β€οΈ"
// Reverse map (emoji β text)
$text = $mapper->reverseMap('β€οΈ'); // ":heart:"
Laravel Translation Integration:
// In a language file (e.g., en.json)
{
"emoji": {
"heart": "β€οΈ",
"smile": "π"
}
}
// Usage
echo trans('emoji.heart'); // Outputs: β€οΈ
Use Case: Cache emoji data to reduce lookup overhead in high-traffic apps. Pattern:
use Symfony\Component\Emoji\EmojiData;
use Illuminate\Support\Facades\Cache;
// Cache all emoji data for 24 hours
$emojiData = Cache::remember('emoji.data', now()->addHours(24), function () {
return EmojiData::getAll();
});
// Usage
$smileys = $emojiData['Smileys & Emotion'];
Skin Tone/ZWJ Sequences:
π¨βπ©βπ§βπ¦ (family) may not normalize as expected. Test thoroughly with your appβs use cases.Emoji::normalize() before storage to ensure consistency.Unicode Version Mismatches:
intl extension.Frontend Rendering:
emoji-mart or ensure your appβs font supports all emoji (e.g., Twemoji).Memory Usage:
php vendor/symfony/emoji/Resources/bin/compress to reduce memory footprint.Locale-Specific Quirks:
text locale (for emoji aliases) may not cover all edge cases. Test with non-English user inputs.EmojiMapper or add custom mappings.Validate Emoji Input:
Emoji::isEmoji() to debug invalid inputs:
if (!Emoji::isEmoji($input)) {
dd(Emoji::getAll()); // Inspect available emoji
}
Check Normalization:
dd(Emoji::normalize('π¨βπ©βπ§βπ¦')); // Should return standardized form
Inspect Metadata:
dd(EmojiData::get('Smileys & Emotion'));
Performance Profiling:
EmojiData::getAll() if you frequently access metadata:
Cache::rememberForever('emoji.all', fn() => EmojiData::getAll());
Custom Emoji Mappings:
EmojiMapper to add app-specific aliases:
class CustomEmojiMapper extends EmojiMapper
{
protected function getCustomMappings(): array
{
return [
':app_heart:' => 'β€οΈ',
':app_smile:' => 'π',
];
}
}
Laravel Service Provider:
public function register()
{
$this->app->bind('emoji', function () {
return new Emoji();
});
}
Blade Helpers:
How can I help you explore Laravel packages today?