How to use the replace function in PHP?
In PHP, the replace function is used to search for a specified value in a string and replace it with a new value. Its usage is as follows:
string replace(mixed $search, mixed $replace, mixed $subject [, int &$count])
Description of parameters:
- $search: The string or array of strings to search for and replace.
- $replace: a string or an array of strings used for replacement.
- Subject: The string or array of strings to be replaced.
- $count (optional): the number of replacements will be stored in this parameter.
Return value:
- Return the replaced string or array of strings.
原文:我需要一段激励人心的演讲来激励我的团队。
改写:I need an inspiring speech to motivate my team.
$str = "Hello World!";
$newStr = str_replace("World", "PHP", $str);
echo $newStr; // 输出:Hello PHP!
In the example above, we used the str_replace function to replace “World” with “PHP” in the string.