printf/sprintf 使用数组参数而不是变量
就像
$args = (
'var1' => 'abc',
'var2' => 'def',
);
$text = sprintf('first var is: %1$s, second one is: %2$s', $args);
基本上 $args
变量是动态的,我不知道它是否有任何条目或有多少。
文本也是如此,我不知道是否需要替换其中的变量,或者有多少。
我怎样才能创建一个这样的函数,以某种方式提取元素并在文本中替换它们,如果像 %s
%d
, %1$s< /code> 等存在吗?
Like
$args = (
'var1' => 'abc',
'var2' => 'def',
);
$text = sprintf('first var is: %1$s, second one is: %2$s', $args);
Basically the $args
variable is dynamic, I don't know if it will have any entries or how many.
The text too, I don't know if it will require variables to be replaced in it, or how many.
How can I make a function like this, that somehow extracts the elements and replaces them in the text if stuff like %s
%d
, %1$s
etc is present?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能需要
vsprintf()
。You probably want
vsprintf()
.