vsprintf, , ( 1) : %n$s ( n - ):
$matches = [ "123", "987", "121"];
$result = 'My phone number is %1$s and police number is %3$s';
$res = vsprintf($result, $matches);
, , $s .
(vprintf vsprintf , printf sprintf , )
Python, preg_replace_callback:
$matches = [ "123", "987", "121"];
$result = 'My phone number is {0} and police number is {2}';
$res = preg_replace_callback('~{(\d+)}~', function ($m) use ($matches) {
return isset($matches[$m[1]]) ? $matches[$m[1]] : $m[0];
}, $result);
, , strtr :
$matches = [ "123", "987", "121"];
$result = 'My phone number is {0} and police number is {2}';
$trans = array_combine(
array_map(function ($i) { return '{'.$i.'}'; }, array_keys($matches)),
$matches
);
$result = strtr($result, $trans);