opis/string
Lightweight PHP string utilities from the Opis ecosystem. Provides helpful helpers for common string operations like trimming, case conversion, searching, slicing, and formatting, with a simple API suited for everyday use in Laravel and standalone projects.
Install via Composer: composer require opis/string. The package provides a single OpisString class (aliased as Opis\String\StringObject) that wraps any string in an immutable, multibyte-aware object. Start by replacing basic string operations (e.g., strlen, strpos, substr) with method calls on the object—for example:
use Opis\String\StringObject;
$s = new StringObject('Café');
echo $s->length(); // 4 (not 5, handles UTF-8 correctly)
echo $s->substring(0, 3); // 'Caf'
Look at the class methods first—especially length(), substring(), strpos(), lower(), upper(), trim(), and contains()—which all handle multibyte characters natively without needing the mb_* extension (though it’s used if available).
upper(), trim()) return a new StringObject, enabling fluent chains:
$result = (new StringObject('hello world'))
->upper()
->substring(0, 5)
->trim();
// $result is 'HELLO'
StringObject in method signatures to enforce multibyte-safe string handling:
public function process(StringObject $title): StringObject { ... }
StringObject::of($str) before operations. Useful in value objects or DTOs where string integrity matters (e.g., user names, titles with accents, emojis).equals(), startsWith(), endsWith(), matches() for robust conditional logic (e.g., validating localized prefixes/suffixes).$str = $str->upper() is required—chaining without assignment discards changes silently.__toString() by default: Convert back to string explicitly with (string)$obj or $obj->toString(). Forgetting this causes silent failures or "Object of class Opis\String\StringObject could not be converted to string" errors.for iterations over large arrays). Prefer bulk operations (e.g., map with StringObject::of($str) once per element).grapheme_* functions or external libraries.StringObject in docblocks to prevent auto-completion fatigue: /** @var StringObject $title */.How can I help you explore Laravel packages today?