Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Emoji Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Install the package via Composer:
    composer require symfony/emoji
    
  2. Compress the dataset (optional but recommended for performance):
    php vendor/symfony/emoji/Resources/bin/compress
    
  3. Basic usage in Laravel:
    use Symfony\Component\Emoji\Emoji;
    
    // Check if a string contains emoji
    Emoji::isEmoji('😊'); // true
    
    // Get emoji by name
    Emoji::get('heart_eyes'); // "😍"
    

First Use Case: Emoji Validation

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
}

Key Classes

  • 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: β†’ "😊").

Implementation Patterns

1. Validation Workflows

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.');
            }
        }],
    ];
}

2. Emoji Lookup and Normalization

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();
    });
}

3. Emoji Picker/Autocomplete

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: 😍

4. Localization and Text Aliases

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: ❀️

5. Caching for Performance

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'];

Gotchas and Tips

Pitfalls

  1. Skin Tone/ZWJ Sequences:

    • Complex emoji like πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦ (family) may not normalize as expected. Test thoroughly with your app’s use cases.
    • Fix: Use Emoji::normalize() before storage to ensure consistency.
  2. Unicode Version Mismatches:

    • The package aligns with the latest Unicode CLDR, but older PHP environments (pre-8.1) may struggle with Unicode handling.
    • Fix: Ensure your server supports PHP 8.1+ and the intl extension.
  3. Frontend Rendering:

    • This package provides Unicode data only. Emoji may render differently across devices/browsers.
    • Fix: Use a frontend library like emoji-mart or ensure your app’s font supports all emoji (e.g., Twemoji).
  4. Memory Usage:

    • The uncompressed dataset is ~1MB. Compress it with php vendor/symfony/emoji/Resources/bin/compress to reduce memory footprint.
  5. Locale-Specific Quirks:

    • The text locale (for emoji aliases) may not cover all edge cases. Test with non-English user inputs.
    • Fix: Extend the EmojiMapper or add custom mappings.

Debugging Tips

  1. Validate Emoji Input:

    • Use Emoji::isEmoji() to debug invalid inputs:
      if (!Emoji::isEmoji($input)) {
          dd(Emoji::getAll()); // Inspect available emoji
      }
      
  2. Check Normalization:

    • Debug emoji sequences with:
      dd(Emoji::normalize('πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦')); // Should return standardized form
      
  3. Inspect Metadata:

    • Dump emoji data for a specific category:
      dd(EmojiData::get('Smileys & Emotion'));
      
  4. Performance Profiling:

    • Cache EmojiData::getAll() if you frequently access metadata:
      Cache::rememberForever('emoji.all', fn() => EmojiData::getAll());
      

Extension Points

  1. Custom Emoji Mappings:

    • Extend EmojiMapper to add app-specific aliases:
      class CustomEmojiMapper extends EmojiMapper
      {
          protected function getCustomMappings(): array
          {
              return [
                  ':app_heart:' => '❀️',
                  ':app_smile:' => '😊',
              ];
          }
      }
      
  2. Laravel Service Provider:

    • Bind the package to Laravel’s container for easy access:
      public function register()
      {
          $this->app->bind('emoji', function () {
              return new Emoji();
          });
      }
      
  3. Blade Helpers:

    • Create Blade directives for common emoji operations:
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope