嗨希望有人可以帮助我,我有以下代码,但我发送$ data = 11,它发送给我一个1010而不是100,两次替换'1',有人知道如何解决这个问题吗?感谢
$search = ['1','2','3','11','12','13'];
$replace = ['10','25','50','100','250','500'];
return str_replace($search, $replace, $data);
答案 0 :(得分:0)
尝试进行相反的顺序并使用带有$ count检查的foreach,如下所示:
$search = ['13','12','11','3','2','1'];
$replace = ['500', '250', '100','50','25','10'];
for($i=0, $count=0; $i<count($search); $i++) {
$data = str_replace($search[$i], $replace[$i], $data, $count);
if ($count) {
return $data;
}
}