Weave 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

spatie/string is a PHP package for fluent, chainable string handling. Wrap any native string with the string() helper, then chain methods like between, toUpper, and toLower, or use array-style offsets to read and modify individual characters easily.

View on GitHub
Deep Wiki
Context7

spatie/string is a lightweight PHP package that makes working with strings elegant. Wrap any native string with the string() helper, then chain readable methods together—no more nested function calls or temporary variables.

echo string('StartMiddleEnd')->between('Start', 'End')->toUpper(); // "MIDDLE"
  • Fluent API — chain methods like between(), toUpper(), and toLower() in a single expression
  • String helper — instantly wrap native strings with string()
  • Offset access — read and write characters via array syntax (string('hello')[1])
  • Concatenation-friendly — combine results with the familiar . operator
  • Immutable, expressive — clean transformations that read like plain English

Install via Composer: composer require spatie/string.

Frequently asked questions about String
How do I install spatie/string with Composer?
Run composer require spatie/string and you are done; there is no service provider, facade, or config file to register. The package autoloads a single global string() helper plus one Spatie\String\String class, so you can call string('my text') anywhere in a Laravel app or a plain PHP project. Because nothing is wired into the container, removing it later is just a composer remove.
Which Laravel and PHP versions does spatie/string support?
Version 3.x requires PHP 8 or newer and is framework-agnostic, so it runs alongside any modern Laravel release including Laravel 9, 10, and 11. The package has no Laravel version constraint of its own, which also means it gets no Laravel-specific testing. If you are stuck on PHP 7, v2.2.3 is the terminal branch and should be treated as unmaintained.
Is spatie/string a replacement for Laravel's Str::of() or the str() helper?
Not really. Illuminate\Support\Str and its Stringable class already cover most of the same chaining API, so on any Laravel 7+ app you would be adding a second, overlapping string vocabulary rather than new capability. spatie/string earns its keep only if you specifically want methods like between(), tease(), possessive(), or segment() in one fluent line. For most projects Str::of() is the better default.
How do I get the part of a string between two markers with spatie/string?
Wrap the source with the helper and chain between(): string('StartMiddleEnd')->between('Start', 'End') returns 'Middle'. You can keep chaining from there, for example ->toUpper() to output 'MIDDLE', and concatenate the result with the normal . operator. It is a compact alternative to strpos and substr boilerplate for one-off parsing.
Does spatie/string handle UTF-8 and multibyte characters correctly?
The README does not document an mb_* guarantee for methods such as between(), tease(), and toUpper() or toLower(), so multibyte behavior is not something you should assume. Before using it on user-facing content with accented characters, CJK text, or emoji, write a quick test with real strings and confirm the output. If multibyte correctness is critical, native mb_* functions or Laravel's Str helpers are safer.
Is spatie/string safe and actively maintained for production use?
It is MIT licensed, published by Spatie, and stable, but the README says it is primarily built for their own projects, which means best-effort fixes and no SLA. Expect a feature-frozen package with occasional patch releases rather than active development. Mechanically it is low risk because there is almost no integration surface, but you should pin the version and treat upgrades as deliberate.
Can spatie/string conflict with another global string() function?
Yes. The helper is a globally autoloaded function declared in a Composer files entry, so if any other package or legacy helper already defines string(), PHP will throw a fatal redeclare error. The probability is low but the failure is catastrophic and hard to trace back in a large dependency graph. Grep your vendor directory and legacy code for existing string() definitions before adopting it.
Does spatie/string mutate objects when I assign to an offset?
Yes, it implements ArrayAccess with mutable-by-offset semantics, so $string = string('gray'); $string[2] = 'e'; changes the wrapper in place and ->toUpper() then outputs 'GREY'. That means it is not a clean immutable value object, and chained code that assumes immutability can surprise reviewers. Reading offsets, like string('hello')[1]->toUpper(), is safe; just be careful with writes.
Is spatie/string a good fit for generating excerpts or teasers from HTML?
tease() trims the string, collapses double spaces, strips HTML, and truncates on word boundaries, which is convenient for plain-text excerpts. It is risky where markup or HTML entities must survive, so verify the output against real CMS content rather than trusting it blindly. If you need entity-safe or markup-aware truncation, use a dedicated library or Laravel's Str::limit with a proper sanitizer.
What are the alternatives to spatie/string?
Laravel's built-in Str::of() and Stringable support, plus the str() helper, are the closest options and require no extra dependency. Outside Laravel, danielstjules/stringy is a fuller-featured fluent string library. The README also mentions Anahkiasen/underscore-php, but that package is effectively abandoned, so do not adopt it or rely on underscore chaining.
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.
jinas/dhivatar
store-doc/laravel-storage-driver
plus-it/laravel-translation-manager
diephp/perhaps
diephp/telegraph
osmaviation/spreadsheet
microsoft/fluentui-system-icons
mmccook/php-json-canonicalization-scheme
middlewares/firewall
qubiqx/montapacking
nyoncode/laravel-package-console
pnscripts/filament-broadquill-lite
pnscripts/laravel-sentfold-lite
vleap/mx-sdk-laravel-internal
twidpay/utils
htetmyathlaing/scout-elasticsearch-driver
artisan-toolbox/front-awesome
ls/client-assistant
users-au/laravel-client
cgv/localized-resource