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.
Feature Development:
string('user-input')->slugify()->tease(50).string($input)->trim()->toLower()->replaceFirst(' ', ' ')) to reduce inconsistencies in databases or APIs.string($article)->tease(150, '...')) or auto-format metadata (e.g., string($title)->prefix('[Draft] ')).string($query)->toLower()->removeDiacritics()) before comparison.string('user')->possessive()), or locale-specific formatting (via underscore.php integration).Roadmap Prioritization:
str_replace() calls with a standardized API.Build vs. Buy:
substr() + strtolower() calls) with a maintained, tested library (MIT license, 560+ stars).string($text)->customMethod()->toUpper()) or compose with underscore.php for advanced use cases.between(), segment(), or pop()), which would require ~50+ LOC per feature.Use Cases:
string($productName)->slugify()) or format prices (string($price)->prefix('$')).preg_replace()-heavy code with readable, chainable methods.Adopt When:
string($input)->clean()->validate() vs. nested if/substr blocks).substr() or case-sensitive mismatches).Look Elsewhere If:
symfony/string or egulias/email-validator.mb_*).danielstjules/Stringy (immutable strings) or Anahkiasen/underscore-php (pure functions).*"This package lets us ship text-processing features faster while reducing bugs. For example:
string($title)->slugify()) instead of manual regex.tease() to save space.ROI:
str_replace() or substr() logic.*"spatie/string gives us a fluent, chainable API for string manipulation, integrating seamlessly with Laravel and PHP 8+. Key benefits:
strtolower(str_replace(' ', '-', $str)) with string($str)->toLower()->slugify().preg_replace snippets—methods are documented and tested.slugify()->between('a', 'z')) or add custom methods.tease(), segment()) with zero dependencies beyond PHP.Example Use Cases:
// Before (spaghetti):
$slug = strtolower(preg_replace('/[^a-z0-9]+/', '-', $title));
// After (clean):
string($title)->slugify();
// Before (error-prone):
$preview = substr($text, 0, 100) . '...';
// After (robust):
string($text)->tease(100, '...');
Migration Path:
Alternatives Considered:
Recommendation: Adopt for new features; phase into legacy code where string logic is repetitive or buggy."*
How can I help you explore Laravel packages today?