(PHP) 如何使用值数组替换字符串中的不同子字符串?

(PHP) How to replace different substrings inside string using an array of values?

如何用子字符串数组替换字符串中的一组占位符?

我的代码:

$replaceWith = array('FIAT', 'car');
$message = '{%s} is the best {%s} of the world.';
$finalMessage = str_replace(array_fill(0, count($replaceWith), '{%s}'), $replaceWith, $message);

var_dump($finalMessage);

输出:

string 'FIAT is the best FIAT of the world.' (length=35)

期望的输出:

string 'FIAT is the best car of the world.' (length=34)

提前致谢!

尝试以下:

<?php
$subject = '%s is the best %s of the world.';
$values = array('FIAT', 'car');
echo vsprintf($subject, $values);
?>