6
In PHP times 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 exit will be
My name is Wallace and I’m 26 years old
But at a given moment I needed to have a functionality similar to those that exist in languages like Python
or C#
, where you can repeat in the format string one of the arguments passed.
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 exit would be:
u"My name is Wallace. Yes, my name is Wallace. I am 1 years old"
Note that the parameter "Wallace"
repeated itself without the need to pass it twice.
Using the sprintf
PHP, I need a similar solution. It is possible to do the same operation as in Python PHP
, using the function sprintf
?
Note: I would like to know specifically about the function sprintf
.In this case, user-created functions would not be welcome as a response.
I was surprised to learn that in PHP it had this :D
– Wallace Maxters
And I had even done a function for it '-'
– Guilherme Lautert
@Guilhermelautert No one reads the manual! haha :P
– Laerte
@Laerte I look in the :D O.R.
– Guilherme Lautert