Skip to main content

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 methodsStr::slug($title, '-') — clear for single operations
  • Fluent stringsStr::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

Str::counted() — quantity-aware singular and plural forms

Pluralize a word according to a number and return it together with the formatted count:
The method is also available through fluent strings:
This is useful for English count labels in interfaces. Thousands separators are applied automatically.

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 title
Extract a domain from a URL
Sanitise user input
Class name to file path

Conditional chaining — when()

pipe() — insert an arbitrary callback

Key fluent methods at a glance

Summary

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:
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
Last modified on July 13, 2026