Php str_replace-用另一组字符串替换一组字符串

Php str_replace-用另一组字符串替换一组字符串,php,string,replace,preg-replace,str-replace,Php,String,Replace,Preg Replace,Str Replace,试图编写一个函数来更正一组反字母缩略词的大小写,但看不出如何更符合逻辑 我现在有这个 $str = str_ireplace(" worda ", " Worda ", $str); $str = str_ireplace(" wordb ", " woRrdb ", $str); 等等,这是一个很长的列表 有没有办法让一组字符串替换为一组替换项?又名: worda = Worda wordb = woRdb 我也看到过使用preg_replace的其他示例,但也看不到使用该函数的方法。您可

试图编写一个函数来更正一组反字母缩略词的大小写,但看不出如何更符合逻辑

我现在有这个

$str = str_ireplace(" worda ", " Worda ", $str);
$str = str_ireplace(" wordb ", " woRrdb ", $str);
等等,这是一个很长的列表

有没有办法让一组字符串替换为一组替换项?又名:

worda = Worda
wordb = woRdb

我也看到过使用preg_replace的其他示例,但也看不到使用该函数的方法。

您可以将数组中的单词列表作为

更美,

$searchWords = array("worda","wordb");
$replaceWords = array("Worda","woRrdb");
$str = str_ireplace($searchWords,$replaceWords,$str); 

您可以将数组中的单词列表作为

更美,

$searchWords = array("worda","wordb");
$replaceWords = array("Worda","woRrdb");
$str = str_ireplace($searchWords,$replaceWords,$str); 

嗯,看起来您不想多次正确编写函数
str\u replace
。 因此,这里有一个解决方案:

您可以将数据放入如下数组中:

$arr = array("worda" => "Worda", "wordb" => "woRdb");
希望这对你来说很容易

然后对其使用
foreach
循环:

foreach($arr as $key => $value){
  $str = str_ireplace($key, $value, $str);
}

嗯,看起来您不想多次正确编写函数
str\u replace
。 因此,这里有一个解决方案:

您可以将数据放入如下数组中:

$arr = array("worda" => "Worda", "wordb" => "woRdb");
希望这对你来说很容易

然后对其使用
foreach
循环:

foreach($arr as $key => $value){
  $str = str_ireplace($key, $value, $str);
}

以下是使用关联数组执行此操作的方法:

$words = array('worda' => 'Worda', 'wordb' => 'woRdb');
$str = str_ireplace(array_keys($words), array_values($words), $str);

以下是使用关联数组执行此操作的方法:

$words = array('worda' => 'Worda', 'wordb' => 'woRdb');
$str = str_ireplace(array_keys($words), array_values($words), $str);