Home >Backend Development >PHP Problem >How to replace multiple strings at once in php
php method to replace multiple strings at once: first create a new PHP document and define a string; then use the [str_repalce()] function to replace multiple strings at once, the code is [$newStr = str_replace ($arr1,$arr2,$str);echo ..].
php method to replace multiple strings at once:
1. Create a new PHP document and define a character String, example:
$str = '我有一个苹果,还有一个鸭梨和茄子';
2. Assume that we currently want to replace the two fruits in the string with mangoes and peaches. We need to define an array first, example:
$arr1 = array('苹果','鸭梨');$arr2 = array('芒果','桃子');
3. Use the str_repalce() function to replace multiple strings at one time, example:
$newStr = str_replace($arr1,$arr2,$str);echo $newStr;
4. Save the above content and view the effect in the browser,
5. You can also replace apples and pears with other foods at the same time, example:
$newStr = str_replace($arr1,'Cabbage',$str);
6. Save the above content, view it in the browser, and replace it with a print of a food. You will find that PHP’s str_replace() function is really powerful
If you want to learn more about programming, please pay attention to the php training column!
The above is the detailed content of How to replace multiple strings at once in php. For more information, please follow other related articles on the PHP Chinese website!