Hidden Methods Beginner Since Laravel 9.x

Str::squish()

Collapse all consecutive whitespace (including newlines and tabs) into a single space and trim the result.

Overview

Str::squish() removes all extraneous whitespace from a string, collapsing multiple spaces, tabs, and newlines into a single space.

Usage

use Illuminate\Support\Str;

Str::squish('  laravel   framework  ');
// 'laravel framework'

Str::squish("laravel\n\n  framework");
// 'laravel framework'

When to Use

  • Cleaning user input that may contain extra whitespace
  • Normalizing strings before comparison or storage
  • Processing text content from external sources