In PHP time the function sprintf
. With it we can format the values sequentially passed from the second parameter.
For example:
sprintf('Meu nome é %s e tenho %d anos de idade', 'Wallace', '26')
The output will be
My name is Wallace and I'm 26 years old.
But at some point I needed to have a functionality similar to what exists in languages like Python
or C#
, where you can repeat one of the arguments passed in the formatting string.
For example in Python
s = "Meu nome eh {0}. Sim, o meu nome eh {0}. Eu tenho {1} anos de idade"
s.format('Wallace', 1)
The output would be:
u "My name is Wallace, yes, my name is Wallace, I am 1 year old."
Note that the "Wallace"
parameter was repeated without having to pass it twice.
Using sprintf
of PHP, I need a similar solution. Is it possible to do the same operation of the example in Python in PHP
, using the function sprintf
?
Note : I would like to know specifically about the sprintf
function. In this case, user-created functions would not be welcome as a response.