Php sprintf format string

I know about sprintf, but how can I use the same parameter anywhere? Example

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

- let's say a few parameters, but I want to put the "test" in two places without a duplicate

+6
php printf string-formatting
source share
2 answers

Use numbering notation %$ :

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

Here, both occurrences of %1$s will be replaced by "test" . This can be found on the sprintf() page.

+15
source share

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

+4
source share

All Articles