Vivek Mistry 👋

I’m a Certified Senior Laravel Developer with 6+ years of experience , specializing in building robust APIs and admin panels, frontend templates converting them into fully functional web applications.

Book A Call
  • 29 May, 2025
  • 334 Views
  • String Functions in Laravel

Str::swap() in Laravel

Using this Str::swap() you can find the value from the string and replace the things on which you want to replace.


For example,

use Illuminate\Support\Str;


$string = Str::swap([
    'Tacos' => 'Burritos',
    'great' => 'fantastic',
], 'Tacos are great!');


// Burritos are fantastic!

Benefits,

  1. Cleaner Syntax – You define replacements in a single associative array: ['search' => 'replace'].
  2. Readable Mapping – At a glance, you see what’s being swapped with what.
  3. Laravel Expressiveness – It fits Laravel’s philosophy of writing code that’s more human-friendly.
  4. Efficiency – Since it uses strtr(), it handles multiple replacements in a single pass.

Share: