Replace all matches for a given value in a string in Lavavel 6
1. Now there is a string with the following contents
The event will take place between default and default
2. It is expected to batch replace the default in the string to basic
3. Refer to the str::replaceArray function to replace the given value in the string using the array order
$string = 'The event will take place between default and default';
$replaced = Str::replaceArray('default', ['Basic'], $string);
var_dump($replaced);
exit;
4. Since there is only one element in the array $replace, only one default is finally replaced. as shown in Figure 1
string(51) "The event will take place between Basic and default"
5. Refer to the str::replaceArray function, modify it to the following implementation
$string = 'The event will take place between default and default';
$search = 'default';
$replace = 'Basic';
$segments = explode($search, $string);
$result = array_shift($segments);
foreach ($segments as $segment) {
$result .= $replace . $segment;
}
var_dump($result);
exit;
6. Default in the string is all replaced with basic. as shown in Figure 2
string(49) "The event will take place between Basic and Basic"

