Static string helpers. Namespace Zero\Lib\Support\Str (aliased globally as Str).
use Zero\Lib\Support\Str;Topics: Transforms · Search · Extraction · Replacement · Composition · Identity · Encoding · Pluralization · Casing · Padding · Random · Fluent
Implementation: Concerns/Str/Transforms.php.
StudlyCase the value (also called PascalCase).
Str::studly('make_http_client'); // 'MakeHttpClient'snake_case the value.
Str::snake('MakeHTTPClient'); // 'make_http_client'kebab-case the value.
Str::kebab('MakeHTTPClient'); // 'make-http-client'camelCase the value.
Str::camel('make_http_client'); // 'makeHttpClient'Title Case Each Word.
Str::title('make http client'); // 'Make Http Client'Standard case conversion.
Str::upper('Zero'); // 'ZERO'
Str::lower('Zero'); // 'zero'URL-friendly slug, with ASCII transliteration.
Str::slug('Héllø Wörld'); // 'hello-world'
Str::slug('A B C', '_'); // 'a_b_c'Transliterate to ASCII; non-mappable bytes become $fallback.
Str::ascii('déjà vu'); // 'deja vu'
Str::ascii('🚀 launch', ''); // ' launch'Alias for ascii() with Laravel-compatible signature.
Str::transliterate('héllo'); // 'hello'Implementation: Concerns/Str/Search.php.
Str::contains('queue:email', 'email'); // trueStr::containsAll('queue:email', ['queue', 'email']); // trueStr::containsAny('queue:email', ['http', 'email']); // trueStr::startsWith('cache:foo', 'cache:'); // true
Str::endsWith('image.png', '.png'); // trueStr::startsWithAny('cache:foo', ['queue:', 'cache:']); // true
Str::endsWithAny('a.tar.gz', ['.zip', '.tar.gz']); // trueStr::doesntContain('hello world', 'foo'); // trueStr::doesntStartWith('hello', 'world'); // true
Str::doesntEndWith('hello', '!'); // trueMultibyte mb_strpos.
Str::position('hello', 'l'); // 2Str::substrCount('aaa', 'a'); // 3Implementation: Concerns/Str/Extraction.php.
Str::limit('A very long sentence', 10); // 'A very...'Str::words('one two three four', 2); // 'one two...'Str::substr('framework', 0, 5); // 'frame'Str::after('auth:token', ':'); // 'token'
Str::before('auth:token', ':'); // 'auth'Str::between('[42]', '[', ']'); // '42'Str::afterLast('a/b/c', '/'); // 'c'
Str::beforeLast('a/b/c', '/'); // 'a/b'Str::betweenFirst('[a][b]', '[', ']'); // 'a'Multibyte-safe index access. Negative indexes count from the end.
Str::charAt('hello', 1); // 'e'
Str::charAt('hello', -1); // 'o'
Str::charAt('hello', 99); // falseFirst or last n characters. Negative $limit returns from the tail.
Str::take('hello', 3); // 'hel'
Str::take('hello', -3); // 'llo'Excerpt a phrase out of a longer text, surrounded by omission marks. radius (default 100) sets the window.
Str::excerpt('hello world', 'world', ['radius' => 3]); // '...lo world'First regex group (or full match if no groups).
Str::match('/foo (\w+)/', 'foo bar'); // 'bar'All regex matches (group 1 if present, else full match).
Str::matchAll('/\d+/', 'a 1 b 2 c 3'); // ['1','2','3']Implementation: Concerns/Str/Replacement.php.
Str::replaceFirst('zero', 'one', 'zero zero'); // 'one zero'Str::replaceLast('zero', 'one', 'zero zero'); // 'zero one'Standard str_replace (or str_ireplace).
Str::replace('foo', 'bar', 'foobaz'); // 'barbaz'Replace each occurrence sequentially.
Str::replaceArray('?', ['a','b'], '? and ?'); // 'a and b'Replace only when $subject starts with $search.
Str::replaceStart('foo', 'X', 'foobar'); // 'Xbar'
Str::replaceStart('foo', 'X', 'barbar'); // 'barbar'Str::replaceEnd('bar', 'X', 'foobar'); // 'fooX'Regex replace; $replace may be a callable.
Str::replaceMatches('/\d+/', 'X', 'a 1 b 2'); // 'a X b X'Str::swap(['{name}' => 'Zero'], 'Hello {name}'); // 'Hello Zero'Str::remove('-', 'a-b-c'); // 'abc'Str::substrReplace('hello', 'X', 1, 1); // 'hXllo'Implementation: Concerns/Str/Composition.php.
Ensure the value starts with $prefix (collapsing duplicates).
Str::start('foo', '/'); // '/foo'
Str::start('//foo', '/'); // '/foo'Str::finish('foo', '/'); // 'foo/'
Str::finish('foo//', '/'); // 'foo/'Append the suffix only when missing (no duplicate collapse).
Str::ensureSuffix('storage/logs', '/'); // 'storage/logs/'Str::wrap('hello', '"'); // '"hello"'
Str::wrap('hello', '<b>', '</b>'); // '<b>hello</b>'Str::unwrap('"hello"', '"'); // 'hello'Multibyte-safe reverse.
Str::reverse('hello'); // 'olleh'Collapse all whitespace runs to single spaces and trim.
Str::squish(" a b\t c "); // 'a b c'Str::deduplicate('a b'); // 'a b'Remove the prefix when present.
Str::chopStart('foobar', 'foo'); // 'bar'
Str::chopStart('foobar', ['baz', 'foo']); // 'bar'Str::chopEnd('foobar', 'bar'); // 'foo'Str::trim(' x '); // 'x'
Str::ltrim(' x '); // 'x '
Str::rtrim(' x '); // ' x'Word-boundary-aware Title Case (handles dashes, underscores, and StudlyCase).
Str::headline('a-pretty_title'); // 'A Pretty Title'APA title casing (lower-cases minor words mid-title).
Str::apa('the quick brown fox'); // 'The Quick Brown Fox'Str::initials('John Doe'); // 'JD'
Str::initials('John Doe', '.'); // 'J.D'mask(string $value, string $character, int $index, ?int $length = null, string $encoding = 'UTF-8'): string
Mask a portion of the value.
Str::mask('1234567890', '*', 4); // '1234******'
Str::mask('1234567890', '*', 4, 4); // '1234****90'Implementation: Concerns/Str/Identity.php.
Wildcard match (*).
Str::is('foo*', 'foobar'); // true
Str::is(['admin/*', 'api/*'], 'api/users'); // trueStr::isAscii('hello'); // true
Str::isAscii('héllo'); // falseStr::isJson('{"a":1}'); // true
Str::isJson('not json'); // falseStr::isUrl('https://example.com'); // true
Str::isUrl('https://x', ['ftp']); // false (scheme not allowed)Str::isUuid('550e8400-e29b-41d4-a716-446655440000'); // trueStr::isUlid('01HW2YPK6Z5XZK7B5N8R7F0Q1V'); // trueRegex match (any pattern in the list).
Str::isMatch('/^foo/', 'foobar'); // trueImplementation: Concerns/Str/Encoding.php.
Str::toBase64('hello'); // 'aGVsbG8='Returns '' on decode failure.
Str::fromBase64('aGVsbG8='); // 'hello'Naive English. Implementation: Concerns/Str/Pluralization.php.
Str::plural('apple'); // 'apples'
Str::plural('apple', 1); // 'apple'
Str::plural('city'); // 'cities'
Str::plural('bush'); // 'bushes'Str::singular('cities'); // 'city'
Str::singular('boxes'); // 'box'
Str::singular('apples'); // 'apple'Pluralize the last StudlyCase segment.
Str::pluralStudly('UserPost'); // 'UserPosts'Implementation: Concerns/Str/Casing.php.
Multibyte-safe.
Str::lcfirst('Hello'); // 'hello'
Str::ucfirst('hello'); // 'Hello'Str::ucwords('hello world'); // 'Hello World'Split on uppercase boundaries.
Str::ucsplit('FooBarBaz'); // ['Foo', 'Bar', 'Baz']Multibyte-safe length.
Str::length('ありがとう'); // 5Str::wordCount('a b c'); // 3wordWrap(string $string, int $characters = 75, string $break = "\n", bool $cutLongWords = false): string
echo Str::wordWrap('the quick brown fox', 10, "\n", true);
// "the quick\nbrown fox"Implementation: Concerns/Str/Padding.php.
Str::padLeft('7', 3, '0'); // '007'Str::padRight('7', 3, '0'); // '700'Str::padBoth('core', 8, '-'); // '--core--'Str::repeat('-', 5); // '-----'Implementation: Concerns/Str/Random.php.
RFC4122 v4 UUID.
Str::uuid(); // 'd3b07384-d9a3-4d2c-9f7e-...'Time-ordered UUIDv7. orderedUuid() is an alias for uuid7() with no args.
Str::uuid7(); // '01928a...'
Str::orderedUuid(); // '01928a...'Time-ordered Crockford ULID.
Str::ulid(); // '01HW2YPK6Z5XZK7B5N8R7F0Q1V'Cryptographically random token. Default alphabet is base62.
Str::random(16); // 'k9QzXk7...'
Str::random(8, '0123456789'); // '04823917'password(int $length = 32, bool $letters = true, bool $numbers = true, bool $symbols = true, bool $spaces = false): string
Strong random password.
Str::password(20); // 'Xk9!aZ.fQ$cP|7yL@vRm'Implementation: Concerns/Str/Fluent.php.
Begin a fluent chain. See stringable.md.
Str::of('users.profile-photo')
->replaceLast('.', '/')
->slug('/'); // 'users/profile-photo'The global str($value) is shorthand for Str::of($value).