您可以使用str_replace()函数来替换多条内容。该函数接受三个参数:要查找的字符串、要替换的字符串和要在其中搜索的字符串。例如,如果您希望将字符串中的'a'替换为'b','c'替换为'd',您可以使用以下代码:

$string = 'a b c d e';
$find = array('a', 'c');
$replace = array('b', 'd');
$new_string = str_replace($find, $replace, $string);
echo $new_string;

输出将是:'b b d d e'。

如果您有很多要查找和替换的字符串,可以使用循环来动态生成$find$replace数组。例如:

$string = 'The quick brown fox jumps over the lazy dog.';
$find = array('quick', 'brown', 'fox', 'lazy', 'dog');
$replace = array('slow', 'black', 'cat', 'energetic', 'wolf');

for ($i=0; $i<count($find); $i++) {
  $string = str_replace($find[$i], $replace[$i], $string);
}

echo $string;

输出将是:'The slow black cat jumps over the energetic wolf.'

PHP 多内容替换:使用 str_replace() 函数

原文地址: https://www.cveoy.top/t/topic/lBZE 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录