Documentation Index
Fetch the complete documentation index at: https://kawax.biz/llms.txt
Use this file to discover all available pages before exploring further.
The Str class
Illuminate\Support\Str provides a consistent API for string operations. Instead of memorising the inconsistent argument order of PHP’s built-in string functions, you get a single, uniform interface that also handles multibyte characters correctly.
Laravel offers two styles:
- Static methods —
Str::slug($title, '-')— clear for single operations - Fluent strings —
Str::of($title)->slug('-')->limit(50)— chain multiple transformations left to right
The
str() global helper is equivalent to Str::of(). You can write str('hello')->upper() instead of Str::of('hello')->upper().Case conversion
Use these when normalising database column names, API keys, or user-facing labels.Str::camel() / Str::snake() / Str::kebab() / Str::studly()
Str::lower() / Str::upper() / Str::title()
Slugs and URLs
Str::slug() — URL-friendly slugs
Converts spaces and special characters into a safe URL segment.
Combine slug generation with fluent strings
Trimming and truncating
Str::limit() — truncate by character count
Truncate article previews or comment snippets to a fixed length.
Str::words() — truncate by word count
Searching and checking
Str::contains() — check for a substring
Str::startsWith() / Str::endsWith()
Str::is() — wildcard pattern matching
Replacing and transforming
Str::replace() — simple replacement
Str::replaceArray() — sequential placeholder replacement
Str::replaceMatches() — regex replacement
Str::remove() — delete substrings
Str::squish() — collapse whitespace
Extracting substrings
Str::before() / Str::after()
Str::between() — text between two markers
Str::start() / Str::finish() — ensure a prefix or suffix
Adds the character only when it is not already there — never duplicates.
Str::chopStart() / Str::chopEnd() — remove a prefix or suffix
Masking and security
Str::mask() — redact part of a string
Useful for displaying partially obscured email addresses, phone numbers, or card numbers.
Str::excerpt() — contextual snippet
Extract a passage of text centred around a keyword — handy for search result snippets.
Random strings and identifiers
Str::random() — random alphanumeric string
Str::uuid() / Str::ulid() — unique identifiers
Str::password() — secure password generation
Fluent strings — Str::of()
Str::of() returns a Stringable instance so you can chain methods. Cast the result to string or call toString() when you need a plain string.
Practical chaining examples
Slug from a blog titleConditional chaining — when()
pipe() — insert an arbitrary callback
Key fluent methods at a glance
Summary
Common Str methods at a glance
Common Str methods at a glance
| Method | Purpose |
|---|---|
Str::slug($str) | URL-friendly slug |
Str::limit($str, $n) | Truncate by character count |
Str::contains($str, $needle) | Substring check |
Str::startsWith($str, $needle) | Prefix check |
Str::endsWith($str, $needle) | Suffix check |
Str::replace($search, $replace, $str) | Simple replacement |
Str::camel($str) | Convert to camelCase |
Str::snake($str) | Convert to snake_case |
Str::kebab($str) | Convert to kebab-case |
Str::studly($str) | Convert to StudlyCase |
Str::upper($str) | Uppercase |
Str::lower($str) | Lowercase |
Str::squish($str) | Collapse extra whitespace |
Str::after($str, $search) | Substring after a marker |
Str::before($str, $search) | Substring before a marker |
Str::between($str, $from, $to) | Text between two markers |
Str::mask($str, '*', $index) | Redact part of a string |
Str::random($length) | Random alphanumeric string |
Str::uuid() | Generate a UUID |
Str::of($str) | Start a fluent chain |
Static methods vs fluent strings
Static methods vs fluent strings
Use static methods when you only need one or two transformations:Use fluent strings (
Str::of()) when you want to chain three or more operations, use conditional logic, or prefer reading the transformations top to bottom:Str class vs PHP built-ins
Str class vs PHP built-ins
Prefer
Str over raw PHP functions (strtolower(), substr(), str_replace()) because:- It handles multibyte characters correctly by using
mb_*functions internally - You can chain operations without nesting function calls
- The argument order is consistent across all methods
- It keeps your code style consistent with the rest of Laravel