spatie/string
Fluent string handling for PHP. Wrap strings with string() to get a chainable object with helpers like between(), case conversion, concatenation, and array-offset access for reading/updating characters. Lightweight utility by Spatie, installable via Composer.
tease(), slugify(), and possessive() are universally useful across domains (e.g., CMS, e-commerce, or analytics). No tight coupling to specific business logic.string($text)->slugify()->toUpper()) aligns with Laravel’s expressive syntax (e.g., query builder, collections).composer require spatie/string) and use immediately. No database migrations, routes, or middleware required.underscore-php for advanced methods (e.g., slugify(), camelCase()), reducing the need for additional packages.$string[2] = 'e'), useful for dynamic text editing (e.g., user inputs, templates).underscore-php.possessive() throw exceptions on empty inputs (documented in changelog 2.2.1). Mitigate via input validation.slugify()) may not handle non-Latin scripts perfectly. Test with target locales (e.g., Arabic, CJK).Str::of() in Laravel vs. string() helper).danielstjules/Stringy for feature parity (e.g., Stringy::create()->slug()).Str helper for overlapping functionality.string() helper for dynamic text (e.g., @php echo string($post->title)->tease(50) @endphp).slug column via setAttribute()).string($request->name)->slugify()).string($response->data)->replaceFirst('old', 'new')).spatie/string.Str::slug($title) with string($title)->slugify().string() helper to composer.json aliases or AppServiceProvider.StringService facade for global access (optional).slug via model accessors).string() helper to AppServiceProvider:
use Spatie\String\String;
app()->bind('string', function () {
return new String('');
});
string(null)->method()).underscore-php updates may introduce breaking changes.#spatie-string for community help.tease() are O(n), but acceptable for most use cases.$strings->map(fn ($s) => string($s)->slugify());
| Scenario | Risk | Mitigation |
|---|---|---|
Invalid input (e.g., null) |
Exception thrown | Validate inputs (e.g., filled($input)). |
| Unicode/locale issues | Incorrect slugs or casing | Test with target locales. |
| Chaining too many methods | Memory bloat | Limit chain length or use intermediate vars. |
| Underscore dependency | Breaking changes in underscore |
Pin version in composer.json. |
STRING_HANDLING.md doc with examples (e.g., slugs, teasers).string($text)->slugify()->toLower()).spatie/string").How can I help you explore Laravel packages today?