Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

printf/sprintf with array arguments instead of variables

Like

$args = (  'var1' => 'abc',  'var2' => 'def', );   $text = sprintf('first var is: %1$s, second one is: %2$s', $args); 

Basically the $args variable is dynamic, I don't know if it will have any entries or how many.

The text too, I don't know if it will require variables to be replaced in it, or how many.

How can I make a function like this, that somehow extracts the elements and replaces them in the text if stuff like %s %d, %1$s etc is present?

like image 321
Alex Avatar asked Sep 07 '11 20:09

Alex


1 Answers

You probably want vsprintf().

like image 64
Oliver Charlesworth Avatar answered Sep 25 '22 17:09

Oliver Charlesworth