vsprintf
返回格式化的字符串
PHP 4, PHP 5, PHP 7, PHP 8 及以上版本
vsprintf() 函数使用一个数组中的参数,按照格式字符串格式化输出一个字符串。它类似于 sprintf(),但参数是以数组形式传入,而不是逐个传递。
string vsprintf(string $format, array $args)
返回一个格式化后的字符串。
format: "There are %d monkeys in the %s" args: [5, "tree"]
执行 vsprintf("There are %d monkeys in the %s", [5, "tree"]) 将返回字符串:
"There are 5 monkeys in the tree"
$format = "There are %d monkeys in the %s"; $args = [5, "tree"]; $result = vsprintf($format, $args); echo $result; // 输出:There are 5 monkeys in the tree
该示例中,格式字符串包含两个格式占位符 %d 和 %s,分别用于插入整数和字符串。参数数组中包含两个值,5 和 "tree"。调用 vsprintf 后,返回的字符串将参数正确替换到格式字符串中。