42、PHP 原生魅力 - 字符串 - 用数组替换字符串

作者: 温新

图书: 【原生 PHP 魅力】

阅读: 126

时间: 2024-09-08 00:05:35

使用数组格式化字符串:vsprintf

函数 vsprinf() 类似于 sprintf(),不同的是 vsprintf 接受一个参数数组,而不是一个可变数量的参数(sprintf)。

<?php

$parameters = ['John', 25];
$string = vsprintf('Hi %s, you are %d years old. Good bye my friend %1$s', $parameters);
echo $string . PHP_EOL;

输出如下:

$ php 2-vsprintf.php
Hi John, you are 25 years old. Good bye my friend John
请登录后再评论