...

/

Replacing a Key/Value Pair of Strings within a String

Replacing a Key/Value Pair of Strings within a String

Learn how to replace a key/value pair within a string with the help of practical examples.

We'll cover the following...

The swap helper methods

The swap helper method is another way to replace values in an existing string. The swap method accepts a key/value pair of string replacements as its first argument and the string to replace values as its second.

The behavior of the swap method is quite different from other string replacement methods you might have encountered.

Press + to interact
<?php
/**
* Versions: Laravel 8, 9
*
* @return string
*/
public static function swap(
array $map,
string $subject
);

To highlight the behavior differences, we will create a simple canned email template function. We will first implement this function using the replace helper method. We will use the following email template across all the following ...