- 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.