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().
Str::of()).class EmojiService {
public function parse(string $text): string {
return preg_replace_callback('/:([a-z]+):/i', fn($m) => Emoji::${$m[1]}(), $text);
}
}
@emoji, @emojiList) and JSON-serializable output support frontend/backend integration. Example:
// Blade
@emoji('grinningFace') // Renders 😃
// API Response
return ['emoji' => Emoji::grinningFace()];
EmojiValidator, enabling rules like:
$request->validate(['comment' => 'emoji']);
$this->app->bind(EmojiService::class, fn() => new EmojiService());
👨 → 🧑). Monitor Spatie’s updates for breaking changes in Emoji::all() or country flags.:smile:) or direct Unicode input?Emoji::all():
$emojis = Cache::remember('emoji.all', now()->addDays(7), fn() => Emoji::all());
Emoji::grinningFace() === '😃' or screenshot tests for UI.@emoji('thumbsUp') in views.$request->validate(['message' => 'emoji']).use Spatie\Emoji\Emoji;
protected $casts = [
'reaction' => 'emoji',
];
public function setReactionAttribute($value) {
$this->attributes['reaction'] = Emoji::${$value}();
}
composer require spatie/emoji.Emoji::grinningFace() and Emoji::countryFlag('us') in a sandbox.Emoji:: methods.emoji rule in forms).TEXT columns.| Step | Task | Dependencies |
|---|---|---|
| 1. Setup | Install package (composer require spatie/emoji). |
None |
| 2. Validation | Add EmojiValidator to AppServiceProvider. |
Laravel validation |
| 3. Blade | Replace hardcoded emojis with @emoji() directives. |
Blade templates |
| 4. API | Return emojis as strings in JSON responses. | API routes |
| 5. Shortcodes | (Optional) Build a service to parse :smile: → 😊. |
Regex/string parsing |
| 6. Testing | Write unit tests for emoji methods and integration tests for Blade/API. | PHPUnit/Pest |
composer update spatie/emoji).Emoji::all() (e.g., renamed emojis).Emoji::grinningFace() vs. Emoji::GRINNING_FACE).Emoji::all() for batch operations (e.g., processing 10K comments).| Risk | Mitigation Strategy | Example |
|---|---|---|
| Emoji rendering fails | Fallback to text labels (e.g., :smile:). |
{{ $emoji ?? ':smile:' }} |
| Unicode corruption | Enforce UTF-8 in database/config. | DB::connection()->setEncoding('utf8mb4'); |
| Package abandonment | Fork the repo or migrate to Twemoji. | Monitor GitHub activity. |
| Validation false positives | Whitelist allowed emojis. | EmojiValidator::allowed(['grinningFace', 'thumbsUp']) |
README.md snippet to the project:
## Emoji Support
Use `Emoji::grinningFace()` or `@emoji('grinningFace')` in
How can I help you explore Laravel packages today?