There is no problem not worth solving, and no technology not worth learning!

Replace all matches for a given value in a string in Lavavel 6

字符串中的 default 全部被替换为 Basic

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

由于数组 $replace 中仅包含一个元素,最终仅替换了一个 default
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

字符串中的 default 全部被替换为 Basic
Figure 2

string(49) "The event will take place between Basic and Basic"


PHP / Laravel / Yii2 Legacy Project Maintenance & Long-Term Technical Support

If your PHP / Laravel / Yii2 project is already in production but needs bug fixing, API troubleshooting, performance optimization, developer handover support, or long-term maintenance, feel free to contact me for remote technical support.

Ideal For:
✅ PHP legacy systems without active maintenance
✅ Laravel / Yii2 project bug fixes
✅ Admin panel feature iterations
✅ RESTful API troubleshooting
✅ MySQL / Redis / Nginx performance issues
✅ Long-term remote part-time maintenance

We can start with a small task:
✅ Production error troubleshooting
✅ API issue analysis
✅ Slow query and performance bottleneck diagnosis
✅ Initial code structure review
✅ Deployment environment and log inspection

If you would like to discuss your project, please contact me and mention: PHP Maintenance Consultation.

Contact Me:
Telegram: @shuijingwan
WeChat: 13980074657
Email: shuijingwanwq@gmail.com

评论

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.