vsprintf() accepts arrays with any keys, so the array_shift() technique is unnecessary when writing a printf-type function. Any parameters you require are easily unset from the array you retrieve with func_get_args():

When you use signed or floating keys, vnsprintf searchs for the signed truncated key of the original array

Note¹: vnsprintf does not support for example "%someKeyf" (floating number, key = someKey) or "%+03d" (signed decimal number, key = 3), you should use "%someKey$f" or "%+03$d" respectively.Note²: "%+03d" (or "%1$+03d") will be interpreted as signed zero-padded decimal number

i wrote a short function that make use of vsprintf. It is useful in first coding/debugging of php scripts, because it is called like a simple printf and it display in italic (you can change it of course ;-) ) every debug messages, making it easy to remove them when your code is ready.

Heres a new version of the vnsprintf function, i call it dsprintf, but that should matter.

<?phpfunction dsprintf() {$data = func_get_args(); // get all the arguments$string = array_shift($data); // the string is the first oneif (is_array(func_get_arg(1))) { // if the second one is an array, use that$data = func_get_arg(1); }$used_keys = array();// get the matches, and feed them to our function$string = preg_replace('/\%\((.*?)\)(.)/e','dsprintfMatch(\'$1\',\'$2\',\$data,$used_keys)',$string); $data = array_diff_key($data,$used_keys); // diff the data with the used_keysreturn vsprintf($string,$data); // yeah!}

function dsprintfMatch($m1,$m2,&$data,&$used_keys) { if (isset($data[$m1])) { // if the key is there$str = $data[$m1];$used_keys[$m1] = $m1; // dont unset it, it can be used multiple timesreturn sprintf("%".$m2,$str); // sprintf the string, so %s, or %d works like it should} else { return "%".$m2; // else, return a regular %s, or %d or whatever is used}}$str = "Hello, %(place)s, how is it hanning at %(place)s? %s works just as well";$find = array('place' => 'world','sprintf','not used');echo dsprintf($str, $find); // 'Hello, world, how is it hanning at world? sprintf works just as well'?>