elvanto/litemoji
Lightweight Laravel package for working with emojis: detect and parse emoji characters, convert between emoji and shortcodes, and sanitize or replace emoji in strings. Handy for chat, comments, and user-generated content where emoji handling needs to be simple and fast.
Installation
composer require elvanto/litemoji
No additional configuration is requiredโjust autoload the package.
First Use Case: Convert Unicode to Shortcode
use Elvanto\Litemoji\Litemoji;
$converter = new Litemoji();
$shortcode = $converter->unicodeToShortcode('๐ฅ'); // Returns `:fire:`
First Use Case: Convert Shortcode to HTML
$html = $converter->shortcodeToHtml(':fire:'); // Returns `<img src="..." alt="fire" class="emoji">`
Where to Look First
Emoji Conversion Pipeline Use the package to standardize emoji formats across your app:
$text = "Hello ๐ฅ! Use :fire: in code.";
$shortcodes = $converter->unicodeToShortcode($text); // "Hello :fire:! Use :fire: in code."
$html = $converter->shortcodeToHtml($shortcodes); // Renders emoji images.
Validation & Sanitization Ensure input is safe before conversion:
if ($converter->isValidShortcode(':fire:')) {
$html = $converter->shortcodeToHtml(':fire:');
}
Custom Emoji Handling Extend the default emoji set (see Gotchas for details).
Integration with Laravel Blade Create a helper for views:
// app/Helpers/EmojiHelper.php
use Elvanto\Litemoji\Litemoji;
function emoji($text) {
return app(Litemoji::class)->shortcodeToHtml($text);
}
Use in Blade:
{!! emoji(':fire:') !!}
API Responses Normalize emoji formats in JSON responses:
return response()->json([
'message' => 'Check this out: ' . $converter->unicodeToShortcode('๐')
]);
Unicode Normalization
๐จ๐ฝ). Use normalizedToShortcode() for consistency:
$converter->normalizedToShortcode('๐จ๐ฝ'); // Returns `:man:๐ฝ` (if supported).
HTML Output Quirks
$converter = new Litemoji([
'html_template' => '<img src="{url}" alt="{alt}" class="emoji {size}">'
]);
{url}, {alt}, and {size} placeholders.Shortcode Collisions
:flag: for flags).Performance
$cacheKey = 'emoji_'.md5($text);
return Cache::remember($cacheKey, now()->addHours(1), function() use ($converter, $text) {
return $converter->shortcodeToHtml($text);
});
Check Emoji Existence
if (!$converter->hasShortcode(':nonexistent:')) {
// Handle unknown emoji (e.g., fallback to text).
}
Inspect Mappings Dump the emoji data to understand mappings:
dd($converter->getEmojiData());
Custom Emoji Extension Override the default emoji set by passing a custom array:
$customEmoji = [
'๐ฅ' => ':custom_fire:',
':custom_fire:' => ['alt' => 'custom fire', 'url' => '/path/to/custom-fire.png']
];
$converter = new Litemoji($customEmoji);
$converter = new Litemoji([
'cdn_url' => 'https://your-cdn.com/emoji/'
]);
size key in the HTML template or constructor options.Event Hooks
Extend functionality by subclassing Litemoji and overriding methods like:
public function unicodeToShortcode($text) {
// Pre-process text (e.g., replace custom patterns).
return parent::unicodeToShortcode($text);
}
Custom Storage
Replace the emoji data source by extending the Emoji class or injecting a custom repository.
Laravel Service Provider Bind the converter to the container for dependency injection:
// app/Providers/AppServiceProvider.php
public function register() {
$this->app->singleton(Litemoji::class, function () {
return new Litemoji();
});
}
How can I help you explore Laravel packages today?