Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

String Laravel Package

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.

View on GitHub
Deep Wiki
Context7

String handling evolved

Frequently asked questions about String
How do I install and use spatie/string in a Laravel project?
Install via Composer with `composer require spatie/string`. Wrap strings using the `string()` helper, then chain methods like `string('Hello World')->between('H', 'd')->toUpper()`, which outputs 'ELLO'. No configuration is needed—just start using it in Blade, controllers, or Eloquent queries.
Does spatie/string work with Laravel Blade templates?
Yes. Use it directly in Blade like `@{{ string($post->title)->tease(50) }}` to generate readable previews. It integrates natively with Laravel’s templating engine, replacing manual string operations with fluent syntax.
Can I use spatie/string with Eloquent queries or validation rules?
Absolutely. For queries, use it in `where` clauses like `where('slug', string($input)->slugify())`. In validation, create custom rules with `Rule::custom('contains_keyword', fn($attr) => string($attr)->contains('keyword'))` for dynamic checks.
What Laravel versions and PHP versions does spatie/string support?
The package requires **PHP 8.0+**, which aligns with Laravel 8/9/10. It won’t work with Laravel 7.x or older due to PHP version constraints. Check your `composer.json` for compatibility before installing.
How does method chaining work, and are there any limitations?
Methods like `between()`, `toUpper()`, and `slugify()` chain seamlessly, but some underscore-php methods (e.g., `isEmail()`) return non-string values, breaking chains. Handle these cases explicitly, like `string($email)->isEmail() ?? false`. Empty strings may also trigger exceptions in methods like `possessive()`.
Is spatie/string faster than native PHP string functions like `substr()` or `str_replace()`?
The overhead from object wrapping is negligible for most use cases. For performance-critical paths (e.g., high-throughput APIs), benchmark against native functions. If needed, cache repeated operations or use direct `str_*` functions for micro-optimizations.
Can I replace all my custom string utilities with spatie/string?
Yes, but assess the effort. Replace ad-hoc `str_*` calls (low risk) or refactor custom classes (higher effort). Start with non-critical operations (e.g., formatting) to test adoption. The fluent syntax may require team training if unfamiliar.
What are the alternatives to spatie/string for Laravel?
For fluent string manipulation, consider `danielstjules/Stringy` (more advanced regex support) or stick with `underscore-php` for basic utilities like `slugify()`. Spatie’s package excels in Laravel integration (Blade, Eloquent) and readability, but evaluate your project’s needs.
How do I test code that uses spatie/string in PHPUnit?
Replace native string operations with fluent assertions. For example, test `string($str)->between('Start', 'End')->toUpper()` with `assertEquals('MIDDLE', string('StartMiddleEnd')->between('Start', 'End')->toUpper())`. Mock the `string()` helper if needed for isolated tests.
Does spatie/string work outside Laravel (e.g., in plain PHP or Symfony)?
Yes, it works in any PHP 8+ project. Laravel’s value lies in its Blade/Eloquent integration, but the core utilities (e.g., `slugify()`, `tease()`) are framework-agnostic. Install via Composer and use the `string()` helper as documented.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope