php sprintf format string

我怕爱的太早我们不能终老 提交于 2019-11-30 04:53:43

问题


i know about sprintf but how can i use the same param in any place? example

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

-it say few params, but i want to place 'test' in two place without duplicate


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/4645164/php-sprintf-format-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!