Home  >  Article  >  Backend Development  >  How to replace multiple strings at once in php

How to replace multiple strings at once in php

coldplay.xixi
coldplay.xixiOriginal
2020-10-08 17:26:043610browse

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 ..].

How to replace multiple strings at once in php

php method to replace multiple strings at once:

1. Create a new PHP document and define a character String, example:

$str = '我有一个苹果,还有一个鸭梨和茄子';

How to replace multiple strings at once in php

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('芒果','桃子');

How to replace multiple strings at once in php

3. Use the str_repalce() function to replace multiple strings at one time, example:

$newStr = str_replace($arr1,$arr2,$str);echo $newStr;

How to replace multiple strings at once in php

4. Save the above content and view the effect in the browser,

How to replace multiple strings at once in php

5. You can also replace apples and pears with other foods at the same time, example:

$newStr = str_replace($arr1,'Cabbage',$str);

How to replace multiple strings at once in php

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

How to replace multiple strings at once in php

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn