如何将可变数量的参数传递给 sprintf() 函数? [英] How can I pass variable number of arguments to sprintf() function?

查看:103
本文介绍了如何将可变数量的参数传递给 sprintf() 函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的网页中,如果用户在表单中选择了某些选项,请说

In my web page, if a user choses certain options in a form, say

1. A -  Chosen
2. B -  Chosen
3. C -  Not Chosen

然后,我的脚本中的 sprintf() 函数应该接受该数量的参数 -

Then, sprintf() function in my script should accept that number of arguments -

sprintf("%s %s", valueOf(A), valueOf(B));

如果三个都被选中,那么

If all three are chosen, then

sprintf("%s %s %s", valueOf(A), valueOf(B), valueOf(C));

我怎样才能做到这一点?

How can I achieve this?

推荐答案

你想要的可能是 vsprintf 函数.它接受一个数组作为参数集.所以在你的情况下,你会有这样的事情:

What you want is probably the vsprintf function. It takes an array as the set of arguments. So in your case, you'd have something like this:

$args = <array_of_chosen_options>;
$fmt = trim(str_repeat("%s ", count($args)));
$result = vsprintf($fmt, $args);

这篇关于如何将可变数量的参数传递给 sprintf() 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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