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