[EDIT]After re-reading Shrike's response and then re-reading the man page, I realize that the following is an unnecessary reinvention of the wheel. But it was an interesting exercise anyway -- one that should be ignored by all. 🙂 [/EDIT]
If you want to have arbitrary sequence and repetition of the place-holders, you could do something like:
<?php
function mySprintf()
{
$args = func_get_args();
if(!count($args))
{
user_error(__FUNCTION__."(): No args given");
return false;
}
$string = array_shift($args);
$search = array();
for($ix=1; count($args); $ix++)
{
$search['{'.$ix.'}'] = array_shift($args);
}
return str_replace(array_keys($search), $search, $string);
}
// sample usage:
$format = "This {2} a {1}. It {2} only a {1}.";
$var1 = 'test';
$var2 = 'is';
echo mySprintf($format, $var1, $var2); //outputs: This is a test. It is only a test.
?>