milesj/emojibase
milesj/emojibase provides a complete, versioned emoji dataset with names, keywords, categories, and locale support. Great for building emoji pickers, search, and autocompletion across platforms, with JSON data, shortcodes, and skin tone variants.
Installation
composer require milesj/emojibase
The package provides no configuration—just autoloads JSON datasets and utility classes.
First Use Case: Fetching Emoji Data
use MilesJ\EmojiBase\Facades\EmojiBase;
// Get all emojis as an array
$emojis = EmojiBase::all();
// Get a specific emoji by shortcode (e.g., "😊")
$emoji = EmojiBase::get('smile');
Where to Look First
MilesJ\EmojiBase\Facades\EmojiBase (primary entry point).vendor/milesj/emojibase/resources/data/ (e.g., emojis.json, emoji-sequences.json).EmojiBase::regex() for validation/extraction.// Get emoji details (shortcode, name, unicode, etc.)
$details = EmojiBase::get('heart_eyes')->toArray();
// Render emoji in a Blade view
echo EmojiBase::render('grinning_face'); // Outputs: 😀
// Check if a string contains valid emojis
$isValid = EmojiBase::containsEmoji('Hello 👋!');
// Extract emoji shortcodes from text
$shortcodes = EmojiBase::extractShortcodes('I ❤️ coding!');
// Returns: ['heart_eyes']
// Get emojis for a specific locale (e.g., Japanese)
$jpEmojis = EmojiBase::locale('ja')->all();
// Render emoji with localized name
echo EmojiBase::locale('es')->render('thumbs_up'); // "👍 Pulgar arriba"
// Validate user input for emoji-only strings
$pattern = EmojiBase::regex()->emojiOnly();
preg_match($pattern, '🚀', $matches);
// Extract emoji sequences (e.g., "👨👩👧👦")
$sequences = EmojiBase::regex()->sequences();
// Merge with custom emojis (e.g., for a branded app)
$customEmojis = [
'rocket' => ['unicode' => '🚀', 'name' => 'Custom Rocket']
];
EmojiBase::merge($customEmojis);
Create a custom Blade directive for emoji rendering:
// In AppServiceProvider@boot()
Blade::directive('emoji', function ($expression) {
return "<?php echo \\MilesJ\\EmojiBase\\Facades\\EmojiBase::render({$expression}); ?>";
});
Usage in Blade:
@emoji('smiling_face_with_tear')
use MilesJ\EmojiBase\Facades\EmojiBase;
public function rules()
{
return [
'bio' => [
'string',
function ($attribute, $value, $fail) {
if (EmojiBase::containsEmoji($value) && !EmojiBase::isValid($value)) {
$fail('Invalid emoji used.');
}
},
],
];
}
return response()->json([
'message' => 'Success!',
'emoji' => EmojiBase::get('fire')->toArray(),
]);
Unicode Normalization
EmojiBase::normalize() to ensure consistent output.Locale-Specific Shortcodes
a vs. jp_a for "👋").EmojiBase::locale('ja')->get('a'); // Japanese "wave"
Regex Performance
emojiOnly regex is strict and may slow down large inputs.$cachedRegex = EmojiBase::regex()->emojiOnly();
Custom Emoji Overrides
Deprecated Emojis
deprecated: true).$activeEmojis = EmojiBase::all()->where('deprecated', false);
Inspect Raw Data Dump the full dataset to debug:
dd(EmojiBase::all()->toArray());
Check for Missing Emojis If an emoji isn’t found, verify the shortcode:
$emoji = EmojiBase::get('unknown_shortcode');
if (!$emoji) {
// Shortcode doesn’t exist
}
Unicode Rendering Issues
Regex Debugging Test regex patterns separately:
$pattern = EmojiBase::regex()->emojiOnly();
preg_last_error_msg(); // Check for errors
Custom Data Sources Extend the package by adding your own JSON dataset:
EmojiBase::extend('custom', function () {
return collect(json_decode(file_get_contents('path/to/custom.json'), true));
});
Usage:
EmojiBase::source('custom')->all();
Hooks for Preprocessing
Override the EmojiBase class to modify data before rendering:
class CustomEmojiBase extends \MilesJ\EmojiBase\EmojiBase {
public function render($shortcode) {
$emoji = $this->get($shortcode);
if ($emoji && $emoji->unicode === '🔥') {
return '🔥 (Custom!)';
}
return parent::render($shortcode);
}
}
Bind in AppServiceProvider:
$this->app->bind(\MilesJ\EmojiBase\Contracts\EmojiBase::class, CustomEmojiBase::class);
Event Listeners
Listen for emoji events (e.g., emoji.rendering):
EmojiBase::listen('emoji.rendering', function ($shortcode, $emoji) {
// Log or modify emoji before rendering
});
Testing
Use the EmojiBaseTestCase trait for unit tests:
use MilesJ\EmojiBase\Testing\EmojiBaseTestCase;
class EmojiTest extends EmojiBaseTestCase {
public function testRenderEmoji() {
$this->assertEquals('😊', EmojiBase::render('smile'));
}
}
How can I help you explore Laravel packages today?