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

How to replace multiple strings at once in php

coldplay.xixi
Release: 2023-03-06 14:32:01
Original
3835 people have browsed it

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 = '我有一个苹果,还有一个鸭梨和茄子';
Copy after login

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

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;
Copy after login

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);
Copy after login

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!

Related labels:
Statement of this Website
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template