tar*_*rek 23 php arrays printf
我有一个数组巫婆匹配字符串占位符如下:
"some text %s another text %s extra text %s"
Run Code Online (Sandbox Code Playgroud)
和数组:
$array[0] match the first %s
$array[1] match the second %s
$array[2] match the third %s
Run Code Online (Sandbox Code Playgroud)
我认为可以使用sprintf函数完成如下:
$content = sprintf("some text %s another text %s extra text %s", $array);
Run Code Online (Sandbox Code Playgroud)
但这会返回太少的参数错误,我试图使用implode:
$content = sprintf("some text %s another text %s extra text %s", implode(",",$array));
Run Code Online (Sandbox Code Playgroud)
提前致谢
FTh*_*son 46
用vsprintf
而不是sprintf
.它采用一个数组参数来格式化字符串.
$content = vsprintf("some text %s another text %s extra text %s", $array);
Run Code Online (Sandbox Code Playgroud)
vsprintf
PHP 5.6+中的替代方案
sprintf("some text %s another text %s extra text %s", ...$array);
Run Code Online (Sandbox Code Playgroud)