symfony/polyfill-intl-grapheme
Native PHP polyfill for Intl Grapheme functions when ext-intl isn’t available. Works with UTF-8 strings and provides grapheme-aware length, substring, splitting, extraction, and case-sensitive/insensitive position and search functions.
intl extension is unavailable or PHP 8.6’s native grapheme_strrev() isn’t utilized.intl extension, which is often disabled in shared hosting. The polyfill provides a drop-in replacement for grapheme_strrev() in PHP 8.6+ environments, aligning with Laravel’s long-term PHP version support.strrev() would fail (e.g., "مدم" → "مدم" vs. ASCII "madam" → "damam").Str::graphemeReverse()), reducing boilerplate and ensuring consistency. This lowers the barrier for developers to handle RTL and Unicode text correctly.strrev() (per Symfony benchmarks). This may impact high-throughput text processing (e.g., bulk operations, real-time search).grapheme_strrev() function requires PHP 8.6+, which may limit adoption for Laravel applications still on older PHP versions (e.g., PHP 8.1/8.2).intl extension or a custom solution for grapheme_strrev().symfony/polyfill-intl-normalizer.grapheme_strrev() function. For older PHP versions, the polyfill provides backward compatibility for other grapheme_* functions but does not support grapheme_strrev().intl extension, it still requires UTF-8 encoded strings and may fall back to PCRE for grapheme cluster splitting (which has version-specific limitations, as noted in the changelog).grapheme_strrev()).composer require symfony/polyfill-intl-grapheme:^1.38
Str helper in a service provider (e.g., AppServiceProvider):
use Illuminate\Support\Str;
Str::macro('graphemeReverse', function ($string) {
return grapheme_strrev($string);
});
Str::macro('graphemeLength', function ($string) {
return grapheme_strlen($string);
});
// Blade
{{ Str::graphemeReverse($rtlText) }}
// PHP
$reversed = Str::graphemeReverse("مدم"); // Returns "مدم"
use Illuminate\Validation\Rule;
$validator->addRules([
'username' => [
'string',
Rule::graphemePalindrome(), // Custom rule using grapheme_strrev
],
]);
strrev() calls remain unchanged unless explicitly replaced.grapheme_str_split) may have PCRE version dependencies (e.g., PCRE < 10.44). Test thoroughly in your environment.false (e.g., grapheme_strrev() on invalid UTF-8 returns false as of v1.38.0).Str helper.IntlGraphemeClusterIterator for complex cases).strrev(). For high-scale applications (e.g., real-time search, bulk processing), consider:
How can I help you explore Laravel packages today?