带有数组的php sprintf [英] php sprintf with array

查看:262
本文介绍了带有数组的php sprintf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组女巫匹配字符串占位符,如下所示:

I have an array witch match string placeholders as follow:

"some text %s another text %s extra text %s"

和数组:

$array[0] match the first %s 
$array[1] match the second %s 
$array[2] match the third %s 

我认为可以使用sprintf函数完成此操作,

I thought it could be done using the sprintf function as follow:

$content = sprintf("some text %s another text %s extra text %s", $array);

但这会返回参数太少的错误,我尝试使用爆破:

but this return the too few arguments error, i tried to use implode:

$content = sprintf("some text %s another text %s extra text %s", implode(",",$array));

预先感谢

推荐答案

使用 vsprintf 代替sprintf.它使用一个数组参数来格式化字符串.

Use vsprintf instead of sprintf. It takes an array parameter from which it formats the string.

$content = vsprintf("some text %s another text %s extra text %s", $array);

这篇关于带有数组的php sprintf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆