symfony/string
Symfony String provides an object-oriented API for working with text safely and consistently. Handle bytes, UTF-8 code points, and grapheme clusters with unified string utilities for slicing, normalization, comparison, and more.
Str helper (via symfony/string), ensuring zero friction in adoption. Laravel’s Str::of() method already wraps Symfony\Component\String\String, making this a drop-in upgrade for existing string operations.mb_* functions by unifying bytes, UTF-8 code points, and grapheme clusters (e.g., emoji like 👨🏽🤝👨🏻 treated as single units). This is non-negotiable for i18n, SEO, or user-generated content (e.g., comments, product names).Validator, Serializer, HttpKernel) already used in Laravel, enabling consistent string handling across the stack.Str::of($input)->method(). Example:
// Before (custom, error-prone):
$slug = Str::lower(preg_replace('/[^a-z0-9]+/', '-', $title));
// After (robust, Unicode-safe):
$slug = Str::of($title)->slug();
Str helper already uses Symfony String, so adoption is instantaneous—no migration required for basic use cases. Advanced features (e.g., kebab(), pascal(), TruncateMode) can be opt-in.__sleep/__wakeup → __serialize) is non-disruptive for Laravel users, as Laravel’s Str abstracts these details.👨👩👧👦 as one unit).😂 treated as wide).startsWith()/endsWith() (e.g., strings ending with \0).mb_* functions, with better accuracy for Unicode.Str helper compatibility.Symfony\Component\String\AbstractString with __sleep/__wakeup? If so, plan a deprecation migration to __serialize.strlen(), substr())? These may need updates to use String::getBytes() or String::getGraphemeLength().EnglishInflector and SpanishInflector may reduce custom logic.Str helper (since Laravel 8). No additional dependencies or configuration are needed.composer require symfony/string
(Redundant for Laravel, as it’s a dependency of illuminate/support.)| Step | Action | Effort | Risk |
|---|---|---|---|
| Assessment | Audit string-heavy code for Unicode edge cases (emojis, accents, graphemes). | Low | Low |
| Opt-In Adoption | Replace custom logic with Str::of()->method() (e.g., slug(), kebab()). |
Medium | Low |
| Testing | Update tests for byte-based assertions (e.g., strlen() → getBytes()). |
High | Medium |
| Deprecation Handling | Migrate __sleep/__wakeup to __serialize in custom AbstractString subclasses. |
Low | Low (if applicable) |
| Feature Rollout | Introduce advanced features (e.g., TruncateMode, Spanish inflector). |
Low | Low |
Str helper. No conflicts.Validator, Serializer).TruncateMode) are opt-in.Str::of()->method().// Before:
$slug = Str::lower(Str::slug($title, '-'));
// After:
$slug = Str::of($title)->slug('-');
assertEquals(1, strlen('👨🏽')) → assertEquals(1, Str::of('👨🏽')->getGraphemeLength())).pascal(), kebab(), plural()) for API naming, UI labels, or SEO.TruncateMode for dynamic text truncation (e.g., social media feeds).__sleep/__wakeup implementations to __serialize (if used).__sleep) are gradual and clearly documented.Str helper docs cover 90% of use cases.How can I help you explore Laravel packages today?