Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use the same parameter more than once?

I know about sprintf(), but how can I use the same parameter more than once?

If I use the following code, I get an error about using few parameters.

sprintf("blabla %s 11111 %s", "test");

I want to replace %s with "test" twice.

like image 921
kusanagi Avatar asked Jan 10 '11 09:01

kusanagi


2 Answers

Use the %$ numbered placeholder notation:

sprintf('blabla %1$s 11111 %1$s', "test");

Here, both occurrences of %1$s will be replaced with "test". There is more on this in the sprintf() manual page.

like image 79
BoltClock Avatar answered Sep 19 '22 13:09

BoltClock


This is called "Argument swapping" and documented in example #3 here: http://php.net/sprintf Use "%1$s", to use argument 1, you can use this multiple times within your format string, as shown in Example #4 of php online documentation.

like image 44
Mike Bretz Avatar answered Sep 21 '22 13:09

Mike Bretz