patchwork/utf8
Portable UTF-8 and grapheme cluster handling for PHP. Provides pure-PHP fallbacks for mbstring, iconv, and intl Normalizer/grapheme_* functions plus UTF-8-aware replacements for native string functions, improving reliability across servers.
Str helper and Illuminate\Support\Stringable already handle basic UTF-8, but this package offers grapheme cluster support (e.g., splitting "👨👩👧👦" into 4 clusters), which is not natively supported in PHP’s mb_* functions or Laravel’s core.intl or mbstring) where available, with fallbacks. This aligns with Laravel’s need for efficient text operations in high-traffic applications.mbstring functions deprecated in favor of Stringable).composer require patchwork/utf8, but no Laravel-specific integrations (e.g., service provider, facade).Str::of() and Illuminate\Support\Stringable handle basic UTF-8, but lack grapheme cluster methods.Str::words() fails on emoji flags). This package fills that gap.| Risk Area | Severity | Mitigation Strategy |
|---|---|---|
| Archived Package | High | Fork the repo, backport fixes, or replace with alternatives (e.g., symfony/polyfill-php80). |
| PHP 8.x Compatibility | Medium | Test with PHP 8.1/8.2; use mbstring polyfills if needed. |
| Performance Overhead | Low | Benchmark against mbstring/intl for critical paths. |
| Lack of Laravel Hooks | Medium | Create a custom facade/service provider for Laravel integration. |
| Unicode Edge Cases | Medium | Validate against test suites (e.g., Unicode CLDR data). |
symfony/string.intl extension’s GraphemeIterator)?mbstring/intl usage that this could resolve?mb_* functions.Stringable integration.Stringable but may need custom wrappers.mbstring reliance.mbstring (enabled by default in PHP) or intl (for grapheme clusters).Str::of(), mb_strlen()).patchwork/utf8 in a sandbox.GraphemeStr::split() vs. Str::words()).use Patchwork\UTF8\GraphemeStr;).Str::graphemes()).mbstring calls in favor of patchwork/utf8 where applicable.| Component | Compatibility Notes |
|---|---|
| Laravel Core | No native integration; requires custom wrappers. |
| PHP Extensions | Prefers mbstring/intl; falls back to pure PHP. |
| Database | UTF-8 storage (e.g., utf8mb4) is assumed; no direct impact. |
| Frontend | Useful for API responses (e.g., grapheme-aware JSON payloads). |
| Third-Party | May conflict with other UTF-8 libraries (e.g., symfony/string). |
mb_* functions with patchwork/utf8 for grapheme operations.Str helper with grapheme methods (e.g., Str::graphemeSplit()).mbstring calls in new code.mbstring (which has inconsistent behavior across PHP versions).GraphemeStr::substr() failures).php artisan tinker --grapheme-test).intl extension speed (if available).utf8mb4 is used for full Unicode support.| Scenario | Impact | Recovery Plan |
|---|---|---|
| PHP Extension Missing | Pure PHP fallback (slower). | Document performance degradation; consider intl as a hard dependency. |
| Unicode Edge Case | Incorrect grapheme splitting. | Add validation layer (e.g., reject invalid sequences). |
| Package Fork Abandoned | No updates for PHP 8.x. | Migrate to symfony/string or voku/portable-utf8. |
| Laravel Version Incompatibility | Integration breaks. | Isolate behind feature flags; provide polyfills. |
How can I help you explore Laravel packages today?