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(), possessive()) that align with cross-cutting concerns like text normalization, validation, or UI display. Avoids coupling to specific business domains.string('text')->slugify()->toUpper()) mirrors Laravel’s Eloquent query builder and Blade directives, improving developer familiarity and reducing cognitive load.camelCase(), snake_case()), reducing duplication if the team already uses this package.composer require spatie/string) with zero configuration. No database migrations, route changes, or middleware required.spatie/string:^2.2.tease() or slugify() may need locale-specific adjustments (e.g., Unicode normalization). Test with non-ASCII strings (e.g., string('café')->slugify()).possessive()) throw exceptions on empty input. Validate input or wrap calls in if (!empty($string)).isEmail()) break fluent syntax. Document or avoid mixing these in critical paths.App\Helpers\StringHelper be deprecated in favor of spatie/string?String class with formatCurrency()?string('long_text')->tease() vs. custom Str::limit().underscore-php or similar libraries? Reduces ramp-up time.@php echo string($post->title)->tease(50) @endphp).public function getFormattedNameAttribute() { return string($this->name)->title(); }).string($request->input)->slugify()).return string($user->bio)->tease(100)->toArray()).strtolower(), substr(), or preg_replace() with fluent methods (e.g., string($text)->toLower()->between('start', 'end')).Str helper where appropriate (e.g., prefer Str::of($text)->slug() over string($text)->slugify() to avoid duplication).spatie/laravel-medialibrary (e.g., string($file->name)->slugify() for filenames) or laravel-excel (e.g., formatting cell data).truncate() helper with string($text)->tease().str_replace() → replaceFirst()).php artisan ide-helper:generate) to autocomplete String methods.@deprecated in favor of spatie/string.@deprecated Use spatie/string instead to old functions.tests/Feature/StringUtilsTest.php).public function test_slug_generation()
{
$this->assertEquals(
'hello-world',
string('Hello World')->slugify()
);
}
spatie/string:^3.0).spatie/string:^2.2 (PHP 7.4+).composer.json and publish facade (if needed).StringService facade for global access (optional):
// app/Providers/AppServiceProvider.php
public function boot()
{
app()->singleton('string', function () {
return new \Spatie\String\String('');
});
}
docs/string-utils.md.## String Utility Adoption
- Before: `strtolower(str_replace(...))`
- After: `string($text)->toLower()->replaceFirst(...)`
String class for domain-specific needs (e.g., app/Extensions/StringExtension.php).How can I help you explore Laravel packages today?